The output to below main is [5,4,3,2,1] [1,2,3,4,5]
Which makes no sense to me. yes, the first time I ran "reverseArrayIteratively", I'm definitely expecting [5,4,3,2,1] so that's the correct output, but after the second function (reverseArrayRecursively), I was expecting it to also return [5,4,3,2,1] because Java is supposed to be "pass by value", so I didn't expect "reverseArrayIteratively" to actually change around the values of myIntArray. I thought that myIntArray inside the main will stay [1,2,3,4,5] no matter how many times I call reverseArrayIteratively or reverseArrayRecursively. What's going on here?
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] myIntArray = {1,2,3,4,5};
System.out.println(Arrays.toString(reverseArrayIteratively(myIntArray, 0, myIntArray.length-1)));
System.out.println(Arrays.toString(reverseArrayRecursively(myIntArray, 0, myIntArray.length-1)));
}
public static int[] reverseArrayIteratively(int[] array, int first, int end) {
while (first < end) {
int temp = array[end];
array[end] = array[first];
array[first] = temp;
first++;
end--;
}
return array;
}
public static int[] reverseArrayRecursively(int[] array, int first, int end) {
int temp = array[end];
array[end] = array[first];
array[first] = temp;
first++;
end--;
if (first < end) {
reverseArrayRecursively(array, first, end);
}
else {
return array;
}
return array;
}