Streams
- In Java 8+ you can make a stream of your
intarray. Call eitherArrays.streamorIntStream.of. - Call
IntStream#boxedto use boxing conversion fromintprimitive toIntegerobjects. - Collect into a list using
Stream.collect( Collectors.toList() ). Or more simply in Java 16+, callStream#toList().
Example:
int[] ints = {1,2,3};
List<Integer> list = Arrays.stream(ints).boxed().collect(Collectors.toList());
In Java 16 and later:
List<Integer> list = Arrays.stream(ints).boxed().toList();
Answer from mikeyreilly on Stack OverflowStreams
- In Java 8+ you can make a stream of your
intarray. Call eitherArrays.streamorIntStream.of. - Call
IntStream#boxedto use boxing conversion fromintprimitive toIntegerobjects. - Collect into a list using
Stream.collect( Collectors.toList() ). Or more simply in Java 16+, callStream#toList().
Example:
int[] ints = {1,2,3};
List<Integer> list = Arrays.stream(ints).boxed().collect(Collectors.toList());
In Java 16 and later:
List<Integer> list = Arrays.stream(ints).boxed().toList();
There is no shortcut for converting from int[] to List<Integer> as Arrays.asList does not deal with boxing and will just create a List<int[]> which is not what you want. You have to make a utility method.
int[] ints = {1, 2, 3};
List<Integer> intList = new ArrayList<Integer>(ints.length);
for (int i : ints)
{
intList.add(i);
}
Videos
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);
}
With Java 8 you can do it in one line:
int[] intArr = { 1, 1, 2, 3, 5, 8, 11};
List<Integer> list = Arrays.stream(intArr).boxed().collect(Collectors.toList());
list.forEach(System.out::println);
First of all, for initializing a container you cannot use a primitive type (i.e. int; you can use int[] but as you want just an array of integers, I see no use in that). Instead, you should use Integer, as follows:
ArrayList<Integer> arl = new ArrayList<Integer>();
For adding elements, just use the add function:
arl.add(1);
arl.add(22);
arl.add(-2);
Last, but not least, for printing the ArrayList you may use the build-in functionality of toString():
System.out.println("Arraylist contains: " + arl.toString());
If you want to access the i element, where i is an index from 0 to the length of the array-1, you can do a :
int i = 0; // Index 0 is of the first element
System.out.println("The first element is: " + arl.get(i));
I suggest reading first on Java Containers, before starting to work with them.
More simple than that.
List<Integer> arrayIntegers = new ArrayList<>(Arrays.asList(1,2,3));
arrayIntegers.get(1);
In the first line you create the object and in the constructor you pass an array parameter to List.
In the second line you have all the methods of the List class: .get (...)
You can use the following instruction:
new ArrayList<>(Arrays.asList(array));
Given:
Element[] array = new Element[] { new Element(1), new Element(2), new Element(3) };
The simplest answer is to do:
List<Element> list = Arrays.asList(array);
This will work fine. But some caveats:
- The list returned from asList has fixed size. So, if you want to be able to add or remove elements from the returned list in your code, you'll need to wrap it in a new
ArrayList. Otherwise you'll get anUnsupportedOperationException. - The list returned from
asList()is backed by the original array. If you modify the original array, the list will be modified as well. This may be surprising.