将字符串输入数组

时间:2014-10-25 21:15:32

标签: java arrays string

我试图将这些字符串输入到二维数组中,但我不确定如何解析字符串。如何获取每个单独的字符串并将其输入数组。

static String[] initStrings =
{
     "...../...\\",
     "..\\.......",
     "......./..",
     "..........",
     "........\\.",
     "..........",
     "..........",
     ".....\\../.",
     "..\\....../",
     ".........."
};

static void initialize() {
    NumRows = initStrings.length;
    NumCol = initStrings.length;
    squares = new int[NumRows] [NumCol];
}

}
}

2 个答案:

答案 0 :(得分:1)

这是2D数组的格式:

static String[][] initStrings =
    {
     {"...../...\\"},
     {"..\\......."},
     {"......./.."},
     {".........."},
     {"........\\."},
     {".........."},
     {".........."},
     {".....\\../."},
     {"..\\....../"},
     {".........."}
    };

答案 1 :(得分:0)

你最好试试这个: -

static String array[][]={{"...../...\\","..\\.......","......./..","...........","........\\.",},{"..........","..........",".....\\../.","..\\....../",".........."}};

试一试。 并且,如果要显示元素,请使用以下代码段: -

for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
System.out.print(array[i][j]);
}
System.out.println();
}