I have a variable of type List<double[][]>
An example of what would be in the list is as follows (with each new line being a list element):
{ {1.0,0.0,0.0}, {0.0,0.0,0.0} }
{ {0.0,0.0,0.0}, {1.0,0.0,0.0}, {0.0,1.0,0.0} }
The outcome I need to achieve is a double[][]
with all the elements from the list as such (using the same values as above):
{ {1.0,0.0,0.0}, {0.0,0.0,0.0}, {0.0,0.0,0.0}, {1.0,0.0,0.0}, {0.0,1.0,0.0} }
The size of the inner most array will always be the same. However, the outter array varies in size.
I'd really appreciate some help on this one, I seem to think the solution is easy but I just can't come up with it!
This is what the Streaming API's flatMap
is for:
List<double[][]> list = Arrays.asList(
new double[][]{{1.0, 0.0, 0.0}, {0.0, 0.0, 0.0}},
new double[][]{{0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}});
double[][] result = list.stream()
.flatMap(Arrays::stream)
.toArray(double[][]::new);
Produces { {1.0,0.0,0.0}, {0.0,0.0,0.0}, {0.0,0.0,0.0}, {1.0,0.0,0.0}, {0.0,1.0,0.0} }
List<double[][]> list;
//calculate the length of the array (sum of the length of all double[][]s in the list)
int resultLen = 0;
for(double[][] d : list)
resultLen += d.length;
double[][] result = new double[resultLen][];
//copy all double[]s that are in the list (wrapped into double[][]s)
//into the new double[][]
int index = 0;
for(double[][] d : list)
for(double[] a : d)
result[index++] = a;
return result;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.