1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Java - printing string array

Discussion in 'Java' started by noirnuit, Jan 25, 2010.

  1. noirnuit

    noirnuit New 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!
     
  2. kiddo

    kiddo New Member

    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: Jan 25, 2010
  3. noirnuit

    noirnuit New Member

    Yes it DOES work! Thank you sooooo much!!! This has saved me a lot of time :D
     

Share This Page