1

Im learning arrays and Im trying to reverse the order in an array. This is the method I've got this far but it's only working for the first half of the values in the array. What am I doing wrong?

public static void reverse(int[] anArray)
{
    int[] a = anArray ;
    for (int j = 0; j <= (a.length - 1); j++ )
    {
        anArray[j] = a[(anArray.length - j - 1)];
    }
}
5
  • 1
    once you stop learning arrays please use Collections.reverse Commented Nov 11, 2011 at 14:06
  • ok, Im not sure what that is yet but since were covering arrays in my university course I have to finish learning this first :(. Commented Nov 11, 2011 at 14:07
  • 1
    Before writing any code, see if you can write down/sketch out the steps that would have to happen for an array to be reversed. Commented Nov 11, 2011 at 14:08
  • yah basically logically it should be like: 1. duplicate the array as a new array (a). 2. Loop through each value in the initial array and make the nth value be nth value from the end of the duplicated array. Is there anything else to it? logically that's all you need, no? Commented Nov 11, 2011 at 14:10
  • possible duplicate of How do I reverse an int array in Java? Commented Apr 11, 2014 at 8:19

9 Answers 9

5

You need to make a temporary location in order to do the swap. This code write the end of the array to the front, but by the time you get to writing the end of the array, the first half of the values have already been lost.

public static void reverse(int[] a)
{
    int l = a.length;
    for (int j = 0; j < l / 2; j++)
    {
        int temp = a[j];
        a[j] = a[l - j - 1];
        a[l - j - 1] = temp;
    }
}

Additionally, you are incrementing j twice. The last part of the for loop definition does the increment between each loop. You don't need to do it manually inside the loop block.

Sign up to request clarification or add additional context in comments.

4 Comments

Yah that was a copying error on my part, that doesnt fix the error though.
@Cody: Check my edit that shows how to fix the real error with an in-place swap.
You accidentally removed the initiation of l when you edited your post now.
@Marcus: This is what happens when you try to clean up code before breakfast, fixed.
2

This is not a difficult problem, and I'm sorry that you've received several wrong answers already.

Your goal is to reverse the contents of the existing array. Your idea is to assign to each array index from the corresponding one in order to switch them all around. However, this would only work if all the assignments happened simultaneously. The code actually runs one assignment at a time. Thus, when you get to the second half of the array, the indices that you're copying from have already been altered by previous runs of the loop.

To fix this, you need to swap pairs of indices. Loop up to halfway (since otherwise you'll swap each pair twice, and end up where you started); take the two indices and swap them. Now, for the reason we just discussed, you can't just assign one to the other and then vice-versa. Instead, you need to "remember" one of the values in a variable first. Assign into that index (since you have the old value saved) from the other one, and then finally assign the remembered value to the other index.

2 Comments

This is correct if you are requried to reverse the array in place, without creating a new one.
The OP appears to be setting his own requirements.
1

When you write int[] a = anArray it will not be a copy of the array but a reference to the very same array. That's why it only works for the first half of the array since the when the second half of the array the first part has already been overwritten. To fix this you have to write:

int[] a = anArray.clone();

4 Comments

oooh, ok, that makes more sense. Thank you
The explanation is correct but the proposed solution is dubious. If you just make a copy of the array and then apply the same algorithm to the copy, then you haven't made any progress. Further, the OP apparently does want to modify the original array.
The OP assign the values to anArray thus operating on the original array.
What's up with the down vote on this one? It works perfectly even though it's not the most optimized way to reverse an array.
1
public static void swap (int[] a, int pos1, int pos2)
{
      int tmp = pos1;
      a[pos1] = a[pos2];
      a[pos2] = tmp;
}

public static void reverse (int[] a)
{
    int l = a.length;
    for (int j = 0; j < (l / 2); j++)
    {
        swap (a, j, l - j - 1);
    }
}

The variable l is just for brevity - it is not a recommendation.

  • The arrays content is mutable in Java, so you don't need a second one, which would only be a new reference to the same array.

Sorry - I missed to swap, of course.

8 Comments

This will only work for the first part of the array aswell and leave the second half untouched.
Okay, and now the second half of the array?
Which would be fine if you'd actually written a swap.
But in the process when switching you will overwrite the values later needed (if you don't use some temporary place to store the value). But in your case you don't even do a swap
Oh - of course, I have to use "swap", not assignment. How foolish to publish without test! Thank you.
|
1

If you are able to use Integer array, you could do like this:

    Integer[] anArray = {0,2,4,1}; // an array of Integer because List do not accept int as type.

    List<Integer> list = Arrays.asList(anArray);  // converting the array to List

    Collections.reverse(list); // reversing the "list" variable.

You can also use Guava's Lists.reverse(List) to create a view backed by the given list that is in reverse order without copying it or modifying the original list.

1 Comment

Awesome, you can reverse an array with Arrays.asList! I thought it was read-only, but it's write-through. The only thing it doesn't support is length-changing operation (add/remove).
1

This reverses the source array by an in place technique. Basically, you are ONLY allowed to use the source array and cannot create another one.

public static void reverseArrayInPlace(int [] source){
    for(int col=0; col<source.length/2; ++col){         
        int temp=source[col];
        source[col]=source[(source.length-1)-col];
        source[(source.length-1)-col]=temp;
    }
}

//initially; 10 20 30 40 50 60

// col=0; 60 20 30 40 50 10

// col=1; 60 50 30 40 20 10

// col=2; 60 50 40 30 20 10

// col=3; 60 50 30 40 20 10

// col=4; 60 20 30 40 50 10

// col=5; 10 20 30 40 50 60

This is why we give the condition col<(source.length/2), if it went up to col<source.length then it would be arranged in the same manner it was before i.e, it wouldn't reverse. Try it both ways to see it yourself.

Comments

0
public static void reverse(int[] anArray)
{
 int[] newArray = new int[anArray.length];
 int index = 0;
 for (int b = anArray.length - 1; b >= 0; b++)
 {
  newArray[index] = anArray[b];
  index++;
 }
}

Start an index off at 0, and take the last element from the "anArray" and put it at the index in the newArray;

1 Comment

newArray is not returned, so this does a bunch of work and throws it way at the end of the method scope. Also, you are incrementing b, while checking for when b becomes negative.
0

Here is the classic example for reversing using stack!

public static void main(String[] args) {
    int[] myIntArray = { 10, 20, 30, 40, 50, 60 };
    int [] reversedArray= new int[myIntArray.length];
    int reversedPosition=0;
    Stack<Integer> s = new Stack<>();
    for (int integer : myIntArray)
        s.push(integer);
    while (!s.empty()) {
        reversedArray[reversedPosition]=s.pop();
        reversedPosition++;
    }
}

Comments

-1

Your problem is that you're reading the values out of the same array you're modifying.

public static void reverse(int[] originalArray) {
    int[] newArray = new int[originalArray.length];

    for (int i = 0; i < originalArray.length; i++) {
        newArray[i] = originalArray[originalArray.length - i - 1];
    }
}

3 Comments

... which won't accomplish anything in the end, because the newArray is not returned and thus not usable anywhere. The OP wants to modify the original array.
Yep, good point. Can do this instead: public static void reverse(int[] originalArray) { int[] newArray = originalArray.clone(); for (int i = 0; i < originalArray.length; i++) { originalArray[i] = newArray[newArray.length - i - 1]; } }
That is another valid approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.