2014-07-13 156 views
4

因此,我試圖用一個循環遍歷二維數組中的所有元素。從1D獲取二維數組索引

這裏是我所在的地方:

public class Test { 
    private static final String[][] key = { 
     {"`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Backspace"}, // 0 - 13 
     {"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\"},  // 0 - 13 
     {"Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter"},  // 0 - 12 
     {"Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "\u2191"},   // 0 - 11 
     {" ", "<", "\u2193", ">"}              // 0 - 3 
    }; 

    public static void main(String[] args) { 
     final int totalLen = 57; 
     String str = ""; 

     for (int i = 0, row = 0, col = 0; i < totalLen; ++i, ++col) { 

      if (row < key.length && i % key[row].length >= key[row].length - 1) { 
       ++row; 
       col = 0; 
       System.out.println(str); 
       str = ""; 
      } 

      if (row < key.length) 
       str += col + " "; 
     } 
    } 
} 

我評論索引範圍的每一行,上面的程序應輸出,但它不一樣的邏輯是錯誤的。有什麼建議麼?

編輯:循環條件必須保持不變。

+0

使用「幻數」(57)和堅持「條件必須保持不變」的要點是什麼?可讀性差,可維護性...如果元素數量不是57,會發生什麼情況?期待一個例外!? – laune

+0

我在其中一個答案的評論中解釋了原因。 '57'不是一個幻數,它是數組中元素的總數。 'i'被用作另一個在這裏無關的數組的索引。 – user2570380

+0

@laune 編輯:「如果元素的數量不是57,會發生什麼?預計會發生異常!?」 首先,你需要冷靜一點。元素的數量是先計算的,所以它不可能不是57.我從這個例子中剝離了不相關的細節。 – user2570380

回答

0

嘗試這種解決方案,它使用從問題的單一while循環和相同的循環條件(如需要):

String str = ""; 
int row = 0, col = 0; 
int i = 0, totalLen = 57; 

while (i < totalLen) { 
    if (col < key[row].length) { 
     str += col++ + " "; 
     i++; 
    } else { 
     System.out.println(str); 
     str = ""; 
     row++; 
     col = 0; 
    } 
} 
System.out.println(str); // prints the last line 

或者,如果你喜歡使用for循環:

String str = ""; 
int totalLen = 57; 

for (int i = 0, row = 0, col = 0; i < totalLen; i++) { 
    str += col++ + " "; 
    if (col == key[row].length) { 
     row++; 
     col = 0; 
     System.out.println(str); 
     str = ""; 
    } 
} 

其中任一片段將在控制檯上產生以下輸出:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 
0 1 2 3 4 5 6 7 8 9 10 11 12 
0 1 2 3 4 5 6 7 8 9 10 11 
0 1 2 3 
+0

@ user2570380看到我更新的答案 –

2

你應該能夠與兩個變量rowcol,其中col在循環頭遞增,並row循環體有條件地遞增做到這一點:

for (int row =0, col = 0 ; row != key.length ; col++) { 
    System.out.println(row + " " + col); 
    if (col == key[row].length-1) { 
     row++; 
     col = 0; 
    } 
} 

循環條件不能不同。對不起,我忘了提到這一點。

您可以添加i和停止,當你達到totalLen,太:

for (int row =0, col = 0, i = 0 ; i != totalLen ; col++, i++) { 
    System.out.println(row + " " + col); 
    if (col == key[row].length-1) { 
     row++; 
     col = 0; 
    } 
} 

然而,這是比較脆弱的,因爲你會得到totalLen的依賴正在正確計算。

+0

這兩種解決方案都會輸出不正確的輸出,OP的代碼打算爲每行產生「col」值,請參閱我的答案。 –

+0

@ user2570380它已經在我的答案:P –

0

好吧,我試圖保持你的循環條件,因爲它...但我沒有利用每個參數。無論如何,這應該工作:

for (int i = 0, row = 0, col = 0; i < totalLen; ++i, ++col) { 
    System.out.println("row " + row + ", col " + col); 
    // what ever you want to do with your loop, do it here 
    // before altering the row and col parameters 

    if (col == key[row].length-1) { 
     col = -1; // if end of the row, set col to -1, it will be incremented in the next loop 
     row++; 
     if(row==key.length){ 
      break; 
     } 
    } 
} 

基本上它是作爲@dasblinkenlight做到了幾乎相同的 - 只有我介紹了一個break條件時row變量達到key數組的長度。您也可以測試if(i==totalLen),但我個人覺得檢查數組長度更可靠。

+0

@ user2570380,你在哪裏,我糾正了它。抱歉 – GameDroids