1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Java - printing string array

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

  1. noirnuit

    noirnuit New Member

    Joined:
    Jan 25, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Apr 11, 2009
    Messages:
    65
    Likes Received:
    1
    Trophy Points:
    0
    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

    Joined:
    Jan 25, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Yes it DOES work! Thank you sooooo much!!! This has saved me a lot of time :D
     

Share This Page