2014-02-05 112 views
0

將字符串放入單個數組很容易,但我想將字符串放入2d char [] []; 我被困在這裏,可以有人幫助我...謝謝你,對不起我的英語不好!將字符串放入2維數組

String woord = "GPDNATSFASELNIERTPOTSRARIRRCOOFPUAUOGONOTORENOTUAMRHRILGTPOFRSCENOIEKLMETANTRSRUNIAARSETEITNAKAVERNTEJLIBFTNVOTWEEDEKLASC"; 

    char[][] bord = new char[11][11]; 
    char[] letters = woord.toCharArray(); 
    int teller = 0; 
    //Board into a single array 
      for (int i = 0; i < woord.length(); i++) { 
       letters[i] = woord.charAt(i); 
       teller++; 
       System.out.print(letters[i]); 
       if (teller % 11 == 0) { 
        System.out.println(); 
       } 
      }  
    //Board into a 2d Array 
    for (int r = 0; r < bord.length; r++) { 
     bord[r][0]=letters[r]; //<=== first 11 letters, next? 
     System.out.print(bord[r][0]); 
     for (int c = 0; c < bord[0].length; c++) { 
       //?? 
     } 
    } 
+0

歡迎SO。請指出與您的問題相關的語言標籤。我已經爲你的問題添加了java。 –

回答

1

您可以在遍歷它時使用常用技巧應用於多維數組。 r*11 + a值(根據循環)將給我們字符串的下一個字符。下面的代碼,

//Board into a 2d Array 
for (int r = 0; r < bord.length; r++) { 
    for(int a = 0; a < 11; a++) 
     bord[r][a] = letters[r*11 + a]; 

    System.out.println(bord[r]); 
} 

會給輸出:

GPDNATSFASE 
LNIERTPOTSR 
ARIRRCOOFPU 
AUOGONOTORE 
NOTUAMRHRIL 
GTPOFRSCENO 
IEKLMETANTR 
SRUNIAARSET 
EITNAKAVERN 
TEJLIBFTNVO 
TWEEDEKLASC