Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create ArrayList (ArrayList<Integer>) from array (int[]) in Java

I have seen the question: Create ArrayList from array

However when I try that solution with following code, it doesn't quite work in all the cases:

import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List;  public class ToArrayList {      public static void main(String[] args) {         // this works         String[] elements = new String[] { "Ryan", "Julie", "Bob" };         List<String> list = new ArrayList<String>(Arrays.asList(elements));         System.out.println(list);          // this works         List<Integer> intList = null;         intList = Arrays.asList(3, 5);         System.out.println(intList);          int[] intArray = new int[] { 0, 1 };         // this doesn't work!         intList = new ArrayList<Integer>(Arrays.asList(intArray));         System.out.println(intList);     } } 

What am I doing wrong here? Shouldn't the code intList = new ArrayList<Integer>(Arrays.asList(intArray)); compile just fine?

like image 492
tuxdna Avatar asked Jul 08 '13 07:07

tuxdna


People also ask

How do you create an Integer ArrayList in java?

ArrayList<Integer> arl = new ArrayList<Integer>(); For adding elements, just use the add function: arl. add(1); arl.

Can you make an ArrayList of int arrays?

ArrayList of Int ArraysWe can create an ArrayList where each element itself is an array. We use the data type and square brackets to create a new array. Similarly, we defined the type of the ArrayList by using int[] . We cannot use primitives like int as ArrayList type, but we can use int[] .

How do you add an Integer array to an ArrayList in java?

An array can be converted to an ArrayList using the following methods: Using ArrayList. add() method to manually add the array elements in the ArrayList: This method involves creating a new ArrayList and adding all of the elements of the given array to the newly created ArrayList using add() method.


1 Answers

The problem in

intList = new ArrayList<Integer>(Arrays.asList(intArray)); 

is that int[] is considered as a single Object instance since a primitive array extends from Object. This would work if you have Integer[] instead of int[] since now you're sending an array of Object.

Integer[] intArray = new Integer[] { 0, 1 }; //now you're sending a Object array intList = new ArrayList<Integer>(Arrays.asList(intArray)); 

From your comment: if you want to still use an int[] (or another primitive type array) as main data, then you need to create an additional array with the wrapper class. For this example:

int[] intArray = new int[] { 0, 1 }; Integer[] integerArray = new Integer[intArray.length]; int i = 0; for(int intValue : intArray) {     integerArray[i++] = intValue; } intList = new ArrayList<Integer>(Arrays.asList(integerArray)); 

But since you're already using a for loop, I wouldn't mind using a temp wrapper class array, just add your items directly into the list:

int[] intArray = new int[] { 0, 1 }; intList = new ArrayList<Integer>(); for(int intValue : intArray) {     intList.add(intValue); } 
like image 138
Luiggi Mendoza Avatar answered Oct 08 '22 09:10

Luiggi Mendoza