I was trying to reverse elements in a character array using the Collections.reverse() method in java. It wasn't reversing them so i took an integer array as was used in the example i was referring to with no success
Here is my code
int[] intarr = {1,2,3,5,7,8};
System.out.println(Arrays.toString(intarr));
Collections.reverse(Arrays.asList(intarr));
System.out.println(Arrays.toString(intarr));
It returns two lists same as the input! what's going wrong here?
Arrays.asList(intarr)
doesn't do what you think it does.
Given that intarr
is a primitive array, it is not possible to simply turn this into a list; after all, a List<int>
is not (yet) legal java.
Thus, that makes a List<int[]>
(as in, a list of int arrays), with exactly 1 element in it: Your int array. Reversing a 1-size list doesn't do anything as you might surmise.
There is no one-liner answer to this short of using third party libraries.