How do I convert int[] into List in Java?
Of course, I\'m interested in any other answer than doing it in a loop, item by it
Here is a generic way to convert array to ArrayList
ArrayList toArrayList(Object o, Class type){
ArrayList objects = new ArrayList<>();
for (int i = 0; i < Array.getLength(o); i++) {
//noinspection unchecked
objects.add((T) Array.get(o, i));
}
return objects;
}
Usage
ArrayList list = toArrayList(new int[]{1,2,3}, Integer.class);