Arrays (Cont.)


Two examples of multi-dimensional arrays are given next:

MultiArray1.java (a multi-dimensional array)
public class MultiArray1 {
  public static void main( String[ ] args ) {
    int[ ][ ]  mArray = new int[5][5];
    for ( int i = 0; i < 5; i++ )
      for ( int j = 0; j < 5; j++ )
        mArray[i][j] = i*5 + j;
    System.out.println( mArray[ Integer.parseInt( args[0] ) ]
                              [ Integer.parseInt( args[1] ) ] );
  }
}
shell> java MultiArray1    

Output:           Result:

MultiArray2.java (another multi-dimensional array)
public class MultiArray2 {
  public static void main( String[ ] args ) {
    int[ ][ ]  mArray = { {  0,  1,  2 },
                          {  3,  4,  5 },
                          {  6,  7,  8 },
                          {  9, 10, 11 },
                          { 12, 13, 14 } };
    System.out.println( mArray[ Integer.parseInt( args[0] ) ]
                              [ Integer.parseInt( args[1] ) ] );
  }
}
shell> java MultiArray2    

Output:           Result:




      I am a nobody, nobody is perfect, therefore I am perfect.