2012-02-21 47 views
1

我只想在方法調用中傳遞2 d數組的列。我知道如何逐行通過2 d,那就是傳遞2 D數組的列作爲Java中的參數

Check(a[i],9);記住a被定義爲2維數組。

但是我不知道如何通過連續做這一行......這樣做不是在這給錯誤

Check(a[i][],9); 

感謝

回答

1

您不能以這種方式訪問​​2d數組中的'列',至少不能使用Java。您需要手動迭代行並選擇所需的列值。

1

你不能做到這一點沒有明確創建數組和通過元素從原始矩陣元素複製。

1

不知道我正確理解你的問題,但我認爲你錯過了第二個循環。

for(int i = 0; i < a.length; i++) 
{ 
    for(int j = 0; j < a[i].length; j++) 
    { 
     cellAtRowIColumnJ(a[i][j], 9) //what is the 9 for? 
    } 
} 

你可能也想這樣(不知道)不知道如果這編譯,這個想法是列值複製到一個新的數組,並傳遞

int[] cols = new int[a.length]; 
for(int i = 0; i < a.length; i++) 
{ 
    cols[i] = a[i][9]; 
} 
callWithColumns(cols); 
1

一個二維數組是不是一個矩陣。它的行爲更像一個數組數組。

int a[][]; 
for (int b[] : a) 
    for (int c : b) 
     System.out.print(c); 

你可能在尋找的什麼是由每個內陣列的第一元件,其不能被自動訪問的陣列。你需要創建一個新的數組。

int temp[] = new int[a.length]; 
for (int x = 0; x < temp.length; x++) 
    temp[x] = a[x][0]; 
1

我想你需要創建一個數組,像這樣,然後把它傳遞:

int column = 0; // column you want to get 
int[] col = new int[a.length]; 
for(int i = 0; i < a.length; i++) { 
    col[i] = a[i][column]; 
} 

// col is now what you want to pass. 
Check(col, 9);