[Solved] Reversing an array by java mechanism [duplicate]


You can reverse an array like this:

public void reverse(Object [] a){
    for(int i = 0; i < a.length / 2; i++){
        Object temp = a[i]; // swap using temporary storage
        a[i] = a[a.length - i - 1];
        a[a.length - i - 1] = temp;
    }
}

It’s worthy to note that it doesn’t matter if the array length is an odd number, as the median value will remain unchanged. I have to admit that I haven’t tested this but it should work.

Working Example

solved Reversing an array by java mechanism [duplicate]