Arrays of Arrays

CSCI 1933 – Introduction to Algorithms and Data Structures
Adriana Picoral

Arrays of Arrays

Create an array of arrays:

double[][] matrix = new double[10][10];

To iterate, you can get the index or write a for each loop:

for (int i = 0; i < matrix.length; i++) {
  for (int j = 0; j < matrix[0].length; j++) {
    matrix[i][j] = i + j;
  }
}

for (double[] row : matrix) {
  for (double item : row) {
    System.out.println(item);
  }
}

Shallow copy vs. deep copy

The copying methods we saw on the previous set of slides do not implement deep copy of an array of arrays.

  • That means when you copy and array of arrays, you are creating another array with references to the original lists.
double[][] matrix = new double[10][10];
double[][] anotherMatrix = matrix.clone();
System.out.println(anotherMatrix[0][0]); // 0.0
matrix[0][0] = 99;
System.out.println(anotherMatrix[0][0]); // 90.0

Practice

Create a class called TheaterSeating with the following requirements (for all methods, think about what parameters you need):

  • Instance variable: A 2D array of boolean values representing seats (rows and columns) – true means the seat is occupied, false means it’s available.
  • Constructor initializes the seating chart with all seats available
  • countAvailableSeats(params?) - returns the total number of available seats
  • toString()

Practice – continuation

  • bookSeat(params?) - marks a seat as occupied if it’s available, returns true if successful, false if the seat was already taken
  • cancelBooking(params?) - marks a seat as available

Submit your TheaterSeating.java solution to gradescope

Solution

public class TheaterSeating {
    private boolean[][] seating;
    private int available;
    private int rows;
    private int columns;

    public TheaterSeating(int rows, int columns) {
        seating = new boolean[rows][columns];
        this.rows = rows;
        this.columns = columns;
        available = rows * columns;
    }

    public int countAvailableSeats() {
        return available;
    }

    public String toString() {
        String out = "";
        for (int i=0; i < rows; i++) {
            out += i + "\t";
            for (int j=0; j < columns; j++) {
                if (seating[i][j]) out += new String(Character.toChars(0x1F464)) + "\t";
                else out +=  "__\t";
            }
            out += "\n";
        }
        return out;
    }

    public boolean bookSeat(int row, int column) {
        if (row < 0 || column < 0) return false;
        if (row >= rows || column >= columns) return false;
        if (seating[row][column]) return false;
        seating[row][column] = true;
        available--;
        return true;
    }

    public void cancelBooking(int row, int column) {
        if (row >= 0 && row < rows && columns >= 0 && column < columns) {
            if (seating[row][column]) {
                available++;
                seating[row][column] = false;
            }
        }
    }

}

Ragged/Jagged Arrays

  • “Non-rectangular” array (ragged/jagged array) – an array of arrays where each inner array can have a different length
    • .length attribute is part of each sub-array
  • Individual subarrays can be treated “individually”
int[][] ragged = new int[3][]; // declares an array with 3 rows, column size unspecified

// Initialize each row with a different number of columns
ragged[0] = new int[2]; // row 0 has 2 columns
ragged[1] = new int[4]; // row 1 has 4 columns
ragged[2] = new int[3]; // row 2 has 3 columns

Looking back

  • What’s the syntax to create an array of arrays in java?
  • How do you iterate over an array of arrays?
  • What’s the difference between a shallow and a deep copy of an array object?