[Solved] Reverse the order of array in java?


Your reverseArray() method assigns array to the variable revArray, thus the 2 variables pointing the same array in memory.

To avoid that, try something like this:

public static int[] reverseArray(int[] array){
    int[] revArray= Arrays.copyOf(array, array.length);
    int i=0;
    int j=revArray.length-1;
    while(i<=j){
        swap(i,j,revArray);
        j--;
        i++;
    }
    return revArray;
}

1

solved Reverse the order of array in java?