java:创建一个字符串矩阵

时间:2011-08-09 14:48:52

标签: java arrays

我从java开始,我正在寻找如何创建一个数组2d的字符串: 我的数组2d应该包含:

10 20 "OK"
5  30 "KO"
20 100 "NA"
10 60  "OK"

String[][] matrix = new String[i][j];
for(r=0;i<matrix.length; r++) {
 for (int c=0; c<matrix [r].length; c++) {
     System.out.print("10 " + matrix [r][c]);
 }

4 个答案:

答案 0 :(得分:7)

String[][] matrix = { {"10","20","OK"},{"5","30","KO"}, {"20","100","NA"}, {"10","60","OK"} };

答案 1 :(得分:3)

Florin说的是什么,但是简化了for循环:

String [][] matrix =  { {"10","20","OK"}, {"5","30","KO"}, {"20","100","NA"}, {"10","60","OK"} };

for (String [] line : matrix) {
    for (String s: line) {
        System.out.print ("10 " + s);
    }
}

答案 2 :(得分:1)

一切似乎都很好。也许你可以更好地利用java中的每个循环:

String[][] matrix = new String[i][j];
for( String[] rows : matrix) {
 for (String row : rows ) {
     System.out.println("10 " + row );
 }

此致  斯特凡

答案 3 :(得分:0)

@Jigar说什么

String[][] matrix = { {"10","20","OK"},{"5","30","KO"}, {"20","100","NA"}, {"10","60","OK"} };

Plus print:

for(r=0;i<matrix.length; r++) {
 for (int c=0; c<matrix [r].length; c++) {
     System.out.print(matrix [r][c] + " ");
 }
 System.out.println();
}