Hello people of StackOverflow! I have used the searrch option, I've found some related answers but none of them explained why this particular method of reverse ordering of an array in Java doesn't work:
class ReverseOrder
{
public static void main(String[] args)
{
int x[] = {1,2,3,4,5};
int y[] = x;
int i, j;
for(i = 0, j = x.length - 1; i < x.length; i++, j--)
{
y[i] = x[j];
}
for(int b = 0; b < x.length; b++)
{
System.out.println("Inverse order is: " + y[b]);
}
}
}
Why is the result 5,4,3,4,5 instead of 5,4,3,2,1??? It drives me absolutely insane and makes no sense to me. Any help would be greatly appreciated!