nlp
nlp

Reputation: 25

How to Merge two or more multidimensional arrays into single multidimensional array using Java

I need to merge my four 3D arrays into single 3D array in java,

Object[][][] obj1,obj2,obj3,obj4;
obj1 = new Object[7][8][7];
obj2 = new Object[7][8][7];
obj3 = new Object[7][8][7];
obj4 = new Object[7][8][7];

As above, i need one

Object[][][] total = Object[28][32][28];
// total = obj1+obj2+obj3+obj4;

Upvotes: 0

Views: 90

Answers (2)

Sachin
Sachin

Reputation: 31

I noticed that I missed flattening of stream before passing it to collect. Adding "flatMap" resolved the issue.

data1 = Stream.of(data1, data2).flatMap (a -> Arrays.stream(a)).collect(Collectors.toList()).toArray(new String [0][0]);

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201467

Stream the arrays, collect the stream to a List and then convert to an array. Something like,

Object[][][] total = Stream.of(obj1, obj2, obj3, obj4)
        .collect(Collectors.toList()).toArray(new Object[0][0][0]);

Upvotes: 2

Related Questions