Two Dimensional Arrays

7.8 Two Dimensional Arrays

Two-dimensional array is a collection of a fixed number of elements of same data type arranged in rows and columns. Following statement create an array of integers organised in 3 rows and 5 columns :
int[][] numbers = new int[3][5];     

Accessing element in a two-dimensional array

To access one of the elements in a two-dimensional array, you must use both subscripts. For example, the following statement stores the number 20 in numbers[1][2]:
numbers[1][2] = 20;

Initializing a Two Dimensional Array

When initializing a two-dimensional array, you enclose each row's initialization list in its own set of braces. Here is an example:
int[][] values - { {10, 20, 30}, {40, 50, 60}, {70, 80, 90} }; 

Displaying All the Elements of a Two-Dimensional Array

A pair of nested loops can be used to display all the elements of a two-dimensional array, Here is an example :
/**
 *  This program demonstrates a two-dimensional array.
 */
public class TwoDimArray
{
   public static void main(String[] args)
   {
      // Array of size 4x3 to hold integers.
      int[][] values =
      {
         { 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 },
         { 11, 21, 31 }
      };

      // Nested loops to print the array in tabular form.
      for (int row = 0; row < 4; row++)
      {
         for (int col = 0; col < 3; col++)
         {
            System.out.print(values[row][col] + " ");
         }

         System.out.println();    // Print new line.
      }
   }
}
Output :
10 20 30
40 50 60
70 80 90
11 21 31

Length field in two-dimensional array

A two-dimensional array is an array of one dimensional arrays. Figure shows another way of thinking of the numbers array as an array of arrays. Each row of two-dimensional array has a length field that holds the number of columns.
A better approach is to use the array's length fields for the upper limit of the subscripts in the loop test expressions. Here are the modified loops:
// Nested loops to print the array in tabular form
for (int row = 0; row < values.length; row++)
{
   for (int col = 0; col < values[row].length; col++)
   {
      System.out.print(values[row][col] + " ");
   }

   System.out.println();    // Print new line.
}

Common Operation in Two Dimensional Array

Some common operations performed on a two-dimensionals array are reading data into the array, printing data, and finding the largest and/ or smallest element in each row of the array and find the sum of rows and columns of the array. To access the methods to process a two-dimensional array easily, we create the class TwoDimArrayMethods and put these methods in this class.
Program (TwoDimArrayMethods.java)
/**
 * This class contains common methods to
 * manipulate two-dimensional array.
 */
public class TwoDimArrayMethods
{
   public void printMatrix(int[][] matrix)
   {
      for (int row = 0; row < matrix.length; row++)
      {
         for (int col = 0; col < matrix[row].length; col++)
         {
            System.out.printf("%7d", matrix[row][col]);
         }

         System.out.println();
      }
   }

   public void sumRows(int[][] matrix)
   {
      int sum;

      // sum of each individual row
      for (int row = 0; row < matrix.length; row++)
      {
         sum = 0;

         for (int col = 0; col < matrix[row].length; col++)
         {
            sum = sum + matrix[row][col];
         }

         System.out.println("The sum of row " + (row + 1) + " = "
                            + sum);
      }
   }

   public void largestInRows(int[][] matrix)
   {
      int largest;

      for (int row = 0; row < matrix.length; row++)
      {
         // assume that the first element of the row is largest
         largest = matrix[row][0];

         for (int col = 1; col < matrix[row].length; col++)
         {
            if (largest < matrix[row][col])
            {
               largest = matrix[row][col];
            }
         }

         System.out.println("The largest element of row " + (row + 1)
                            + " = " + largest);
      }
   }
}
Program (TwoDimArrayMethodsDemo.java)
public class TwoDimArrayMethodsDemo
{
   public static void main(String[] args)
   {
      TwoDimArrayMethods operate = new TwoDimArrayMethods();
      int[][] board   =
      {
         { 20, 15, 6, 19, 18 }, { 4, 46, 24, 17, 18 },
         { 12, 50, 23, 16, 31 }
      };

      operate.printMatrix(board);
      System.out.println();
      operate.sumRows(board);
      System.out.println();
      operate.largestInRows(board);
   }
}
Output :
     20     15      6     19     18
      4     46     24     17     18
     12     50     23     16     31

The sum of row 1 = 78
The sum of row 2 = 109
The sum of row 3 = 132

The largest element of row 1 = 20
The largest element of row 2 = 46
The largest element of row 3 = 50

No comments:

Post a Comment

Pages