I have a list of int arrays in Java 7.
At the end,I need to flatten out the entire list into a single concatenated int array.
List<int[]> list=new ArrayList<int[]>();
list.add(new int[]{0,1});
list.add(new int[]{2,3});
System.out.println(list.flatten());
Output expected -
int[]{0,1,2,3}
What would be the most elegant way to achieve this?
You can use streams:
removed - Java 7 was sneaked in later!
I don't think you can make it any more elegant than:
int newlen = 0;
for( int[] a: list ){
newlen += a.length;
}
int[] out = new int[newlen];
int pos = 0;
for( int[] a: list ){
System.arraycopy( a, 0, out, pos, a.length );
pos += a.length;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句