2010-01-21 47 views
3

我想加入2個矩陣,列數和行數相同,但我想知道如何用一個命令來做到這一點。我怎樣才能加入Java中的兩個矩陣

我已經知道如何使用做這個,然後,我想知道是否有一個Java命令可以爲我完成這項工作。

例如

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

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

魔術命令來將它們合併成矩陣m

INT M =加入(M1,M2);

m = 

1 2 3 

4 5 6 

7 8 9 

10 11 12 

13 14 15 

回答

3
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); 

您可能要克隆的每一行雖然

+0

對不起夥計,但它並沒有奏效。這種方法沒有接受矩陣作爲參數。 – marionmaiden

+0

對不起,它是arraycopy(小寫字母c) –

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

對不起,但它沒有奏效。這種方法沒有接受矩陣作爲參數。 – marionmaiden

3

的Apache Commons是您的朋友:

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

我看到有很多鏈接在Apache Commons站點上下載。我應該爲此採取什麼? – marionmaiden

+0

apache commons-lang是你在找什麼 – beny23