tags:

views:

108

answers:

3

Hello

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
+2  A: 
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

Maurice Perry
Sorry dude, but it didn't worked. this method didin't accepted an matrix as parameters.
marionmaiden
Sorry, it's arraycopy (with a lowercase c)
Maurice Perry
Now it worked! Thanks
marionmaiden
+1  A: 
int m[][] = Arrays.copyOf(m1, m1.length + m2.length);
System.arraycopy(m2, 0, m, m1.length, m2.length);
Silvio Donnini
Sorry dude, but it didn't worked. this method didin't accepted an matrix as parameters.
marionmaiden
+1  A: 

Apache Commons is your friend:

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

Lluis Martinez
I saw there is a lot of links to donwload in Apache Commons site. Wich one should I take for this?
marionmaiden
apache commons-lang is what you're looking for
beny23