What is best way recopying int[]
array elements into ArrayList
?
I need to do this fast so what would be the fastest way?
What is best way recopying int[]
array elements into ArrayList
?
I need to do this fast so what would be the fastest way?
Use Arrays#asList(T... a)
to create "a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)"
Integer[] intArray = {1, 2, 3, 42}; // cannot use int[] here
List<Integer> intList = Arrays.asList(intArray);
Alternatively, to decouple the two data structures:
List<Integer> intList = new ArrayList<Integer>(intArray.length);
for (int i=0; i<intArray.length; i++)
{
intList.add(intArray[i]);
}
Or even more tersely:
List<Integer> intList = new ArrayList<Integer>(Arrays.asList(intArray));
ArrayList is not the best storage for primitives such as int. ArrayList stores objects not primitives. There are libraries out there for Lists with primitives.
If you really want to store all primitive int
as anInteger
object you will have to convert them one by one using Integer.valueOf(...)
(so you don't create extra Integer instances when not needed).
While you can do this:
List<Integer> list = Arrays.asList(1, 2, 3);
You cannot do this:
int[] i = { 1, 2, 3 };
List<Integer> list = Arrays.asList(i); // this will get you List<int[]>
as an array is actually an object as well an treated as such in var-args when using primitives such as int. Object arrays works though:
List<Object> list = Arrays.asList(new Object[2]);
So something along the lines of:
int[] array = { 1, 2, 3 };
ArrayList<Integer> list = new ArrayList<Integer>(array.length);
for (int i = 0; i < array.length; i++)
list.add(Integer.valueOf(array[i]));
With Guava, you don't need to bother with the Integer[]
:
int[] array;
List<Integer> list = Ints.asList(array);
and if you want an ArrayList
, that's just
List<Integer> list = Lists.newArrayList(Ints.asList(array));
(Disclosure: I contribute to Guava.)
Otherwise, if all you have is an int[]
, you have to do it the old-fashioned way:
int[] array;
List<Integer> list = new ArrayList<>(array.length);
for (int val : array) {
list.add(val);
}
I'll provide an alternate answer as well, depending on how the List is used this could perform better (creation is faster, and only the actually used ints are converted, but they are always converted not just the first time, so repeated get of the same element is worse). Create a simple wrapper list around the array:
final int[] array = {1, 2, 3};
List list = new AbstractList<Integer>() {
public Integer get(int index) {
return array[index];
}
public Integer set(int index, Integer value) {
//or just throw new UnsupportedOperationException("..."); if not needed
Integer old = array[index];
array[index] = value;
return old;
}
public int size() {
return array.length;
}
};
or in a method creating it for you:
static List<Integer> asList(final int[] array) {
return new AbstractList<Integer>() { ...
};
}