Two-Dimensional Arrays

Declaring & Initializing

int[][] nums1 = new int[3][4];
int[][] nums2 = new int[][] {{1, 0, 1}{2, 3, 4}};

for(int i = 0; i < nums1.length; i++) {
   for(int j = 0; j < nums1[i].length; j++) {
      nums1[i][j] = 2 * i + 3 * j;
   }
}

Example: Graphics

Recall that JComponent class has a paintComponent method that programmers can redefine in an extension:

class MyComponent extends JComponent {
   public void paintComponent(Graphics g) {
      g.fillRect(5, 5, 1, 1); // draw a tiny rectangle
   }
}

We can imagine that a Graphics object such as g, contains a two dimensional array of PIXELS. A PIXEL is a dot on the screen that can be colored. Initially, all PIXELS are white:

class Canvas {
   private Color[][] pixels;
   private Color brush = Color.BLACK; // for filling shaps
   private Color pen = Color.BLACK; // for drawing lines
   public Canvas(int width, int height) {
      pixels = new Color[width][height];
      for(int i = 0; i < pixels.length; i++) {
         for(int j = 0; j < pixels[i].length; j++) {
            pixels[i][j] = Color.WHITE;
         }
      }
   }
   public void fillRect(int x, int y, int w, int h) {
      for(int i = x; i < x + w; i++) {
         for(int j = y; j < y + h; j++) {
            pixels[i][j] = brush;
         }
      }
   }

 

   public void drawRect(int x, int y, int w, int h) {
      // ???
   }

   // etc.
}

Example: Matrix Arithmetic

An NxM (N-by-M) matrix is a collection of numbers organized into N rows and M columns. Alternatively, we can think of an NxM matrix as a collection of N M-dimensional vectors. Like numbers and vectors, matrices can be added and subtracted to produce new matrices. Matrices can also be multiplied.

public class MatrixUtils {

   /**
    @returns true if dim(mat) = rows x cols
    */
   public static boolean checkDim(double[][] mat, int rows, int cols)
   {
      if (rows != mat.length) return false;
      for(int row = 0; row < rows; row++) {
         if (cols != mat[row].length) return false;
      }
      return true;
   }

   public static double[][] add(double[][] mat1, double[][] mat2) {

      int rows = mat1.length;
      int cols = mat1[0].length;
      if (!checkDim(mat1, rows, cols) || !checkDim(mat2, rows, cols))
      {
         System.err.println("rows must be same length");
         System.exit(1);
      }
      double[][] result = new double[rows][cols];
      for(int row = 0; row < rows; row++) {
         for(int col = 0; col < cols; col++) {
            result[row][col] = mat1[row][col] + mat2[row][col];
         }
      }
      return result;
   }

   public static void display(double[][] mat) {
      int rows = mat.length;
      int cols = mat[0].length;
      if (!checkDim(mat, rows, cols)) {
         System.err.println("rows must be same length");
         System.exit(1);
      }
      for(int row = 0; row < rows; row++) {
         for(int col = 0; col < cols; col++) {
            System.out.printf(" %4.2f", mat[row][col]);
         }
         System.out.println();
      }
   }

   public static void main(String[] args) {
      double[][] input1 = new double[][]
         {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
      double[][] input2 = new double[][]
         {{4, 5, 6}, {9, 8, 7}, {3, 1, 2}};
      double[][] sum = add(input1, input2);
      display(input1);
      display(input2);
      display(sum);
   }
}

Program Output

1.00 2.00 3.00
4.00 5.00 6.00
7.00 8.00 9.00
4.00 5.00 6.00
9.00 8.00 7.00
3.00 1.00 2.00
5.00 7.00 9.00
13.00 13.00 13.00
10.00 9.00 11.00

Example: Cellular Automata