Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Java - printing string array (http://www.go4expert.com/forums/java-printing-string-array-t20752/)

noirnuit 25Jan2010 12:03

Java - printing string array
 
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!

kiddo 25Jan2010 15:32

Re: Java - printing string array
 
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 :)

noirnuit 25Jan2010 22:11

Re: Java - printing string array
 
Yes it DOES work! Thank you sooooo much!!! This has saved me a lot of time :D


All times are GMT +5.5. The time now is 19:05.