开发者

How can I join two Matrix in Java

I want to join 2 matrix with the same number of columns and different number of lines, but I'm wondering how can I do this with one command.

I already know how to do this using for's, then, I want to know if there is a command in Java that do the job for me.

For example

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

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

Magic command to join 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


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 line though


Apache Commons is your friend:

int m[][] = (int [][])ArrayUtils.addAll(m1, m2);


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜