How to add two matrices in Java

I want to add two matrices with the same number of columns and different numbers of rows, but I want to know how to do this with one command

I already know how to use for, and then I wonder if there is a command in Java that can help me do this

for example

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

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

The magic command adds them to the matrix M

int m = join(m1,m2);

m = 

1 2 3

4 5 6

7 8 9

10 11 12

13 14 15

Solution

int m[][] = new int[m1.length+m2.length][];
int m[][] = new int[m1.length+m2.length][];
System.arraycopy(m1,m,m1.length);
System.arraycopy(m2,m1.length,m2.length);

You may want to clone each row

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>