convert int[] to ArrayList

http://stackoverflow.com/questions/10269300/convert-int-into-arraylist

int[] num = {1, 2, 3};
java.util.List<int[]> l = Arrays.asList( num );

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:

Integer[] num ={1,2,3,42};// cannot use int[] here

List<Integer> intList = Arrays.asList(num);

so the solution is to use a loop

tips

  • new ArrayList<Integer>(); could be new ArrayList<Integer>(intArray.length); to avoid reallocate array
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s