将String放入二维数组中

时间:2014-02-05 15:43:45

标签: java multidimensional-array arrays

将String放入单个数组很简单但我想将字符串放入2d char [] []; 我被困在这里,有人可以帮助我...谢谢你,对不起我的英语不好!

    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++) {
              //??
         }
    }

1 个答案:

答案 0 :(得分: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