Two Dimensional Arrays

6. 2D Array of int


Answer:

Yes, just as for 1D arrays.

If you want a collection of a variety of types, you probably want to define your own class to contain them.

2D Array of int

The following program creates a 2D array of int that implements the gradeTable example. Details about declaring and constructing 2D arrays will be explained later.

The declaration of gradeTable uses an initializer list as a short-cut way to create a 2D array object and place values into it. The list contains 7 rows each separated by a comma; each row is a list of values.


Annotation 2020-03-26 205543class gradeExample
{
  public static void main( String[] arg )
  {
    // declare and construct a 2D array
    int[][] gradeTable = 
        {  {99, 42, 74, 83, 100}, 
           {90, 91, 72, 88, 95},
           {88, 61, 74, 89, 96},
           {61, 89, 82, 98, 93},
           {93, 73, 75, 78, 99},
           {50, 65, 92, 87, 94},
           {43, 98, 78, 56, 99}  };

    System.out.println("grade 0,0: " + gradeTable[0][0]);
    System.out.println("grade 2,4: " + gradeTable[2][4]);

    gradeTable[5][3] = 99 ;

    int sum = gradeTable[0][1] + gradeTable[0][2] ;
    System.out.println( "sum: " + sum );
  }
}


Question 6:

What does the program print out?