3

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]);
 }
1
  • 4
    So far so good. What is your question? Do you want to know how to stuff the string values into the array? Commented Aug 9, 2011 at 14:50

4 Answers 4

7
String[][] matrix = { {"10","20","OK"},{"5","30","KO"}, {"20","100","NA"}, {"10","60","OK"} };
Sign up to request clarification or add additional context in comments.

Comments

4

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);
    }
}

Comments

1

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

Comments

0

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();
}

1 Comment

I got an ArrayIndexOutOfBoundsExceptions , 3 values for each line ?? and when I type String[][] matrix = { {"test","10","20","OK"},...} it works , where is the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.