TWO-DIMENSIONAL ARRAYS

DECLARING TWO-DIMENSIONAL ARRAY

Syntax   data type    arrayname[ ][ ];
Example
double  grades[ ] [ ];

CREATING TWO-DIMENSIONAL ARRAY

Syntax   data type    arrayname[ ][ ] ;
             arrayname = new data type [ROWS][COLS];
             where  ROWS - number of rows
                        COLS   - number of columns per row

Example
double  grades[ ] [ ];
grades  = new double [5][8];

You can combine the declaration and creation process into one step for arrays.

double grades[ ][ ] = new double [5][8];

INITIALIZING TWO-DIMENSIONAL ARRAY

Example   double grades [3][4] = { { 45.0, 89.0, 90.0, 67.0}, { 54.0, 75.0, 34.0, 99.0}, {88.0, 92.0, 10.0, 56.0} };

USING TWO-DIMENSIONAL ARRAY

Example
double grades [3][4] = { { 45.0, 89.0, 90.0, 67.0}, { 54.0, 75.0, 34.0, 99.0}, {88.0, 92.0, 10.0, 56.0} };
for (int row =0; row <grades.length; row++)
   for (int col = 0; col <grades[row].length; col++)
      { grades[row][col]  = grades[row][col] + 35.0;
       }

PASSING A TWO-DIMENSIONAL ARRAY TO A METHOD

        Example
        public class Test
       { public static void main(String[ ] args)
       {
double grades [3][4] = { { 45.0, 89.0, 90.0, 67.0}, { 54.0, 75.0, 34.0, 99.0}, {88.0, 92.0, 10.0, 56.0} };
         display(grades);
       }
        public static void display(double g[3][4])
       {   for (int row =0; row <g.length; row++)
               { for (int col = 0; col <g[row].length; col++)
                    { System.out.println(g[row][col] + "   ");
                     }
                      System.out.println( );
                 }
       }
      }

RETURNING TWO-DIMENSIONAL ARRAY FROM THE METHOD

A method can return a two-dimensional array.

Syntax:  return  arrayname;

Example 
class Test
{ public double [ ] [ ] getGrades( )
  {double grades [3][4] = { { 45.0, 89.0, 90.0, 67.0}, { 54.0, 75.0, 34.0, 99.0}, {88.0, 92.0, 10.0, 56.0} };
    return grades;
  }
}

public class TestArray
{ public static void main(String[ ] args)
   { Test  x = new Test( );
      double  y[ ][ ]  = x.getGrades( );
    }
}