How can I join two matrix in Java

I want to join a matrix with the same number of columns and different number of rows, but I am wondering how I can do this with a single command.

I already know how to do this using for , then I want to know if there is a command in Java that does this work for me.

for example

int m1[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int m2[][] = {{10, 11, 12}, {13, 14, 15}};

Magic team to combine them into a matrix m

int m = join (m1, m2);

m = 

1 2 3

4 5 6

7 8 9

10 11 12

13 14 15
+3
source share
3 answers
int m[][] = new int[m1.length+m2.length][];
System.arraycopy(m1, 0, m, 0, m1.length);
System.arraycopy(m2, 0, m, m1.length, m2.length);

You might want to clone each row though

+3
source

Apache Commons is your friend:

int m[][] = (int [][])ArrayUtils.addAll(m1, m2);
+3
source
int m[][] = Arrays.copyOf(m1, m1.length + m2.length);
System.arraycopy(m2, 0, m, m1.length, m2.length);
+1

Source: https://habr.com/ru/post/1729376/


All Articles