Java - printing string array

noirnuit's Avatar
Newbie Member
Hi, I'm trying to figure out how to print a string array in such a way that the output will have 2 rows and 2 columns.

Here's the portion of my code:

Code:
        String[][] courseArray = {{"art", "science"}, {"history", "mathematics"}};
        System.out.println ("\n" + "Courses taken this semester: " + courseArray);
I don't know how to construct the for-loop. Help will be greatly appreciated. Thank you!
0
kiddo's Avatar
Contributor
to print two dimension array, we must use loop in loop.

Code:
for(condition){
  for(condition){
    // print array
  }
}

Here is the loop example :
Code:
String[][] courseArray = new String({"Art","Science"},{"History","Mathematics"});

for(int i=0; i<courseArray.length; i++){
  for(int j=0; j<courseArray.length; j++){
    System.out.println(courseArray[i][j]);
  }
}
I think it works

Last edited by kiddo; 25Jan2010 at 15:44..
0
noirnuit's Avatar
Newbie Member
Yes it DOES work! Thank you sooooo much!!! This has saved me a lot of time