tags:

views:

731

answers:

8

How do I reverse an int array in Java?

Is this a correct method?

    for(int i = 0; i < validData.length; i++)
    {
        int temp = validData[i];
        validData[i] = validData[validData.length - i - 1];
        validData[validData.length - i - 1] = temp;
    }
+2  A: 
public class ArrayHandle {
    public static Object[] reverse(Object[] arr) {
        List < Object > list = Arrays.asList(arr);
        Collections.reverse(list);
        return list.toArray();
    }

}

Hope this helps.

Braveyard
Was there a problem with my code above to get a thumb down ?
Braveyard
The array's of ints not objects, this won't work.
Tom
+11  A: 

Your method is correct only if you iterate

for(int i = 0; i < validData.length / 2; i++)

The way you do it, you swap each element twice, so the result is the same as the initial list.

3lectrologos
A: 

It is most efficient to simply iterate the array backwards.

I'm not sure if Aaron's solution does this vi this call Collections.reverse(list); Does anyone know?

SilverSun
Iterating backwards over the array requires a new array. I like the solution posted above that does the inline reversing without creating a new array.
Simucal
A: 

Your method reverses the array twice! I.e., you end up swapping the first and last elements twice.

Yoni
+1  A: 

Your program will work for only length = 0, 1. You can try :

int i = 0, j = validData.length-1 ; 
while(i < j)
{
     swap(validData, i++, j--);  // code for swap not shown, but easy enough
}
fastcodejava
Perhaps you meant swap as pseudo code for an inline swap rather than a method call, but if not that won't work. Java passes by reference so it is not possible to write a swap method for variables.
Dean Povey
I meant whatever way you can get v[i]
fastcodejava
+5  A: 

With Commons.Lang, you could simply use

ArrayUtils.reverse(int[] array)

Most of the time, it's quicker and more bug-safe to stick with easily available libraries already unit-tested and user-tested when they take care of your problem.

Manur
+2  A: 

This is the equivalent of Manur's answer, but using guava:

Collections.reverse(Ints.asList(array));
finnw
A: 

I think it's a little bit easier to follow the logic of the algorithm if you declare explicit variables to keep track of the indices that you're swapping at each iteration of the loop.

public static void reverse(int[] data) {
    for (int left = 0, right = data.length - 1; left < right; left++, right--) {
        // swap the values at the left and right indices
        int temp = data[left];
        data[left]  = data[right];
        data[right] = temp;
    }
}

I also think it's more readable to do this in a while loop.

public static void reverse(int[] data) {
    int left = 0;
    int right = data.length - 1;

    while( left < right ) {
        // swap the values at the left and right indices
        int temp = data[left];
        data[left] = data[right];
        data[right] = temp;

        // move the left and right index pointers in toward the center
        left++;
        right--;
    }
}
Bill the Lizard