arraylist to int array conversion in java

Solutions on MaxInterview for arraylist to int array conversion in java by the best coders in the world

showing results for - "arraylist to int array conversion in java"
Sonia
09 Sep 2016
1//only works in java 8 or later
2list.stream().mapToInt(i -> i).toArray();
Maximilian
16 Jul 2020
1int[] ints = {1, 2, 3};
2List<Integer> intList = new ArrayList<Integer>(ints.length);
3for (int i : ints)
4{
5    intList.add(i);
6}
Anna
26 Jan 2020
1templist.stream().mapToInt(i->i).toArray();
Jacopo
26 Oct 2018
1//use this if you don't want to use any methods to convert an
2//arrayList(with the same size as required array) to a 'int[]' type.
3//Also, this is only useful when you know the size of the array you're working with.
4
5  int[] array = new int[size];
6  for (int i = 0; i < array.length; i++) {
7      ans[i] = arraylist.get(i);
8  }
9// array will be now of int[] type
10
11// Also as @FineFinch pointed it could be done using :
12    //only works in java 8 or later
13    list.stream().mapToInt(i -> i).toArray();
Matteo
22 Feb 2017
1Your_ArrayList_Name.stream().mapToInt(k->k).toArray();
Celia
09 Feb 2018
1int[] ints = {1, 2, 3};
2List<Integer> intList = new ArrayList<Integer>(ints.length);
3for (int i : ints)
4{
5    intList.add(i);
6}
7
similar questions
queries leading to this page
arraylist to int array conversion in java