Home > OS >  How convert ArrayList<int[]> to int[] (primitive array)?
How convert ArrayList<int[]> to int[] (primitive array)?

Time:06-25

I has ArrayList:

ArrayList<int[]> al1 = 
   0 = [1,1,1,1,1]
   1 = [0,1,0,1,1]
   2 = [0,0,0,0,0]
...

And function must return int[] like this:

1,1,1,1,1,0,1,0,1,1,0,0,0,0,0

How do it?

CodePudding user response:

I would stream the arraylist and apply a flatmap to the int[](s). Like,

int[] arr = al1.stream().flatMapToInt(IntStream::of).toArray();

CodePudding user response:

Or with help of Guava:

int[] arr = Ints.concat(al1.toArray(new int[][]{}));
  • Related