If you are using java-8 there's also another way to do this.
int[] arr = list.stream().mapToInt(i -> i).toArray();
What it does is:
- getting a
Stream<Integer>from the list - obtaining an
IntStreamby mapping each element to itself (identity function), unboxing theintvalue hold by eachIntegerobject (done automatically since Java 5) - getting the array of
intby callingtoArray
You could also explicitly call intValue via a method reference, i.e:
int[] arr = list.stream().mapToInt(Integer::intValue).toArray();
It's also worth mentioning that you could get a NullPointerException if you have any null reference in the list. This could be easily avoided by adding a filtering condition to the stream pipeline like this:
//.filter(Objects::nonNull) also works
int[] arr = list.stream().filter(i -> i != null).mapToInt(i -> i).toArray();
Example:
List<Integer> list = Arrays.asList(1, 2, 3, 4);
int[] arr = list.stream().mapToInt(i -> i).toArray(); //[1, 2, 3, 4]
list.set(1, null); //[1, null, 3, 4]
arr = list.stream().filter(i -> i != null).mapToInt(i -> i).toArray(); //[1, 3, 4]
Answer from Alexis C. on Stack OverflowIf you are using java-8 there's also another way to do this.
int[] arr = list.stream().mapToInt(i -> i).toArray();
What it does is:
- getting a
Stream<Integer>from the list - obtaining an
IntStreamby mapping each element to itself (identity function), unboxing theintvalue hold by eachIntegerobject (done automatically since Java 5) - getting the array of
intby callingtoArray
You could also explicitly call intValue via a method reference, i.e:
int[] arr = list.stream().mapToInt(Integer::intValue).toArray();
It's also worth mentioning that you could get a NullPointerException if you have any null reference in the list. This could be easily avoided by adding a filtering condition to the stream pipeline like this:
//.filter(Objects::nonNull) also works
int[] arr = list.stream().filter(i -> i != null).mapToInt(i -> i).toArray();
Example:
List<Integer> list = Arrays.asList(1, 2, 3, 4);
int[] arr = list.stream().mapToInt(i -> i).toArray(); //[1, 2, 3, 4]
list.set(1, null); //[1, null, 3, 4]
arr = list.stream().filter(i -> i != null).mapToInt(i -> i).toArray(); //[1, 3, 4]
You can convert, but I don't think there's anything built in to do it automatically:
public static int[] convertIntegers(List<Integer> integers)
{
int[] ret = new int[integers.size()];
for (int i=0; i < ret.length; i++)
{
ret[i] = integers.get(i).intValue();
}
return ret;
}
(Note that this will throw a NullPointerException if either integers or any element within it is null.)
EDIT: As per comments, you may want to use the list iterator to avoid nasty costs with lists such as LinkedList:
public static int[] convertIntegers(List<Integer> integers)
{
int[] ret = new int[integers.size()];
Iterator<Integer> iterator = integers.iterator();
for (int i = 0; i < ret.length; i++)
{
ret[i] = iterator.next().intValue();
}
return ret;
}
Videos
I have noticed the syntax to convert an ArrayList<int[]> to a 2-d array (int[][]) is
arrName.toArray(new int[0][0]);
I am most curious about the parameters that we are sending into the toArray() method, and am curious how 'new int[0][0]' indicates this .
Don't use raw types. Replace ArrayList roomNums = new ArrayList(); with
CopyArrayList<Integer> roomNums = new ArrayList<>();
Then return it as Integer[]
CopyroomNums.toArray(Integer[]::new);
If you need primitive array, then it can be done with stream:
Copyreturn roomNums.stream().mapToInt(Integer::valueOf).toArray();
See also How to convert an ArrayList containing Integers to primitive int array?
Unfortunately, you will need to use Integer[] if you want to use toArray
Copy List<Integer> roomNums = new ArrayList<>();
// code here
Integer[] arr = new Integer[roomNums.size()];
return roomNums.toArray (arr);
However you can still do it manually like
Copy List<Integer> roomNums = new ArrayList<> ();
// code here
int[] arr = new int[roomNums.size()];
for (int i = 0; i < arr.length; i++)
arr[i] = roomNums.get (i);
return arr;
Suppose we have a List<Employee> object and we want to convert it into Employee[ ].
List<Employee> list=new ArrayList<>();
We can convert list to object type array in following ways:
Employee[] empArray = list.toArray(new Employee[0]);
or
Employee[] empArray = new Employee[list.size()];
list.toArray(empArray);
But to convert arrays to primitive types. you have to convert it into following way:-
List<Integer> list = ...;
int[] array = new int[list.size()];
for(int i = 0; i < list.size(); i++) array[i] = list.get(i);
Is there any other way to convert List to its specific type of array?