0
private String [][] personalDetails() {

        logger.debug( "personalDetails()" );

        String personArray [][] =
            {
              { "John",  " Muray", "1-10-1982" },
              { "Peter",  " Kamau", "2-5-1987" },
              { "Mary",  " Wambui", "8-5-1988" },
              { "Bethu",  " Kips", "8-8-1987" },
              { "Cetera",  " Omosh", "2-5-1987" }

            };   
           
          return personArray;

       
    }
  
       /**
     * Demonstrates initializing and populating a multi-dimensional array
     * in one statement
     */
   private void personalDetailsContent( ) {

        logger.debug( "personalDetailsContent()" );
            
             String [][] personArray= new String[ 5 ][5];
              for(int i=0; i<=personArray.length; i++){
                  for(int j=0; j<=personArray[i].length; j++){
                   personArray[i][j]=i;
                  }
                System.out.println( "Names " + i +  "" + j +" : " + personArray[ i ][j] );
              }

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

3
Contributors
3
Replies
6
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0
    Please create a multi dimensional array that stores the following
    information for 5 people:

        * First Name
        * Last Name
        * Date of Birth

    Your application should have one method that populates the multi
    dimensional array with data and another method that shows the contents
    of the multi dimensional array.

Have gone this far:

 private String [][] personalDetails() {

    logger.debug( "personalDetails()" );

    String personArray [][] =
        {
          { "John",  " Muray", "1-10-1982" },
          { "Peter",  " Kamau", "2-5-1987" },
          { "Mary",  " Wambui", "8-5-1988" },
          { "Bethu",  " Kips", "8-8-1987" },
          { "Cetera",  " Omosh", "2-5-1987" }

        };   

      return personArray;


    }

   /**
 * Demonstrates Showing the content a multi-dimensional array
 * in a different method from populating method.
 */

   private void personalDetailsContent( ) {

    logger.debug( "personalDetailsContent()" );

         String [][] personArray= new String[ 5 ][5];

          for(int i=0; i<=personArray.length; i++){

              for(int j=0; j<=personArray[i].length; j++){
                System.out.println( "Person index " + j +" : " + personArray[i][j] );

              }

          }
}

Errror:

Error: Exception in thread 'main' java.lang.IndexOutOfBoundException: 5

Edited by pyTony: fixed formatting

0

Arrays are 0 based,, so a [5] array has length 5 and indexes 0-4
Your loop
for(int i=0; i<=personArray.length; i++){
will attempt to access indexes 0-5, and will fail on the last one with an IndexOutOfBounds (5)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.