java : create a matrix of strings
开发者_开发知识库I begin with java and I'm searching for how to create an array 2d of strings : my array 2d should contains :
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]);
}
String[][] matrix = { {"10","20","OK"},{"5","30","KO"}, {"20","100","NA"}, {"10","60","OK"} };
What Florin said, but with simplified for-loop:
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);
}
}
All seems good. Maybe you could do a better use of for each loops in java :
String[][] matrix = new String[i][j];
for( String[] rows : matrix) {
for (String row : rows ) {
System.out.println("10 " + row );
}
Regards, Stéphane
What @Jigar said
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();
}
精彩评论