Consider the following snippet,
public static void main(String[] args) {
int[] arr = new int[] {1,2,3,4,5,6,7};
Collections.reverse(Arrays.asList(arr));
System.out.println("The value contained in the array is : " + Arrays.toString(arr));
Integer[] arr1 = new Integer[] {1,2,3,4,5,6,7};
Collections.reverse(Arrays.asList(arr1));
System.out.println("The value contained in the array is : " + Arrays.toString(arr1));
String[] strings = new String[] {"tom", "cat", "bob", "boss"};
Collections.reverse(Arrays.asList(strings));
System.out.println("The value contianed the the array now is : " + Arrays.toString(strings));
}
It's returning me the expected result for Integer[]
and String[]
as
The value contained in the array is : [1, 2, 3, 4, 5, 6, 7]
The value contained in the array is : [7, 6, 5, 4, 3, 2, 1]
The value contianed the the array now is : [boss, bob, cat, tom]
but it does not reverse the int[]
. I didn't understand why really. I referred to this SO question (Collections.reverse doesn't work correctly) but it didn't help me really to understand why primitives are not reversed back and populated on original int[] passed.