You're building a seating chart for a theater. Each seat has a row and column. A 2D array lets you store and access data by two coordinates - perfect for grids, game boards, and spreadsheets.

Create a seating chart

Initialize a 2D array to represent a grid of seats.

example
CreateGrid.java
Replay: real traced execution (multi-file project)
public class CreateGrid {
    public static void main(String[] args) {
        // Theater seating chart: rows x seats per row
        int rows = 3;
        int cols = 4;

        // Create 2D array for seat numbers
        int[][] seating = new int[rows][cols];

        // Assign seat numbers (1, 2, 3, ...)
        int seatNumber = 1;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                seating[row][col] = seatNumber;
                seatNumber++;
            }
        }

        // Display the seating chart
        System.out.println("=== Theater Seating Chart ===");
        for (int row = 0; row < rows; row++) {
            System.out.print("Row " + (row + 1) + ": ");
            for (int col = 0; col < cols; col++) {
                System.out.print(seating[row][col] + "\t");
            }
            System.out.println();
        }

        int totalSeats = rows * cols;
        System.out.println("Total seats: " + totalSeats);
    }
}
public class CreateGrid {
    public static void main(String[] args) {
        // Theater seating chart: rows x seats per row
        int rows = 5;
        int cols = 4;

        // Create 2D array for seat numbers
        int[][] seating = new int[rows][cols];

        // Assign seat numbers (1, 2, 3, ...)
        int seatNumber = 1;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                seating[row][col] = seatNumber;
                seatNumber++;
            }
        }

        // Display the seating chart
        System.out.println("=== Theater Seating Chart ===");
        for (int row = 0; row < rows; row++) {
            System.out.print("Row " + (row + 1) + ": ");
            for (int col = 0; col < cols; col++) {
                System.out.print(seating[row][col] + "\t");
            }
            System.out.println();
        }

        int totalSeats = rows * cols;
        System.out.println("Total seats: " + totalSeats);
    }
}
public class CreateGrid {
    public static void main(String[] args) {
        // Theater seating chart: rows x seats per row
        int rows = 3;
        int cols = 6;

        // Create 2D array for seat numbers
        int[][] seating = new int[rows][cols];

        // Assign seat numbers (1, 2, 3, ...)
        int seatNumber = 1;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                seating[row][col] = seatNumber;
                seatNumber++;
            }
        }

        // Display the seating chart
        System.out.println("=== Theater Seating Chart ===");
        for (int row = 0; row < rows; row++) {
            System.out.print("Row " + (row + 1) + ": ");
            for (int col = 0; col < cols; col++) {
                System.out.print(seating[row][col] + "\t");
            }
            System.out.println();
        }

        int totalSeats = rows * cols;
        System.out.println("Total seats: " + totalSeats);
    }
}
  1. rows ← 3, cols ← 4, seatNumber ← 1

    2public class CreateGrid {3    public static void main(String[] args) {4        // Theater seating chart: rows x seats per row5        int rows→ 3 = 3;    //@var=_,56        int cols→ 4 = 4;    //@var=_,67        8        // Create 2D array for seat numbers9        int[][] seating = new int[rows][cols];  //?seats10        11        // Assign seat numbers (1, 2, 3, ...)12        int seatNumber→ 1 = 1;13        for (int row = 0; row < rows; row++) {
  2. for (int row = 0; row < rows; row++)

    pass 1 of 3
    12int seatNumber = 1;13for (int row0 = 0; row < rows3; row++) {14    for (int col = 0; col < cols; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. seating[row][col] ← 1, seatNumber ← 2

    pass 1 of 12
    13for (int row = 0; row < rows; row++) {14    for (int col0 = 0; col < cols4; col++) {15        seating[row][col]→ 1 = seatNumber1;16        seatNumber→ 2++;17    }
    All 12 passes — pass 1 is the card above
    passcolrowseating[row][col]seatNumber
    1000 11 2
    2100 22 3
    3200 33 4
    4300 44 5
    5010 55 6
    6110 66 7
    7210 77 8
    8310 88 9
    9020 99 10
    10120 1010 11
    11220 1111 12
    12320 1212 13
  4. System.out.println("=== Theater Seating Chart ===");

    20// Display the seating chart21System.out.println("=== Theater Seating Chart ===");22for (int row = 0; row < rows; row++) {
    output=== Theater Seating Chart ===
  5. for (int row = 0; row < rows; row++)

    pass 1 of 3
    21System.out.println("=== Theater Seating Chart ===");22for (int row0 = 0; row < rows3; row++) {23    System.out.print("Row " + (row0 + 1) + ": ");24    for (int col = 0; col < cols; col++) {
    outputRow 1: 
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  6. for (int col = 0; col < cols; col++)

    pass 1 of 12
    23System.out.print("Row " + (row + 1) + ": ");24for (int col0 = 0; col < cols4; col++) {25    System.out.print(seating[row][col]1 + "\t");26}
    output1	
    All 12 passes — pass 1 is the card above
    passcolseating[row][col]row
    1010
    2120
    3230
    4340
    5051
    6161
    7271
    8381
    9092
    101102
    112112
    123122
  7. System.out.println();

    26    }27    System.out.println();28}
  8. System.out.println();

    26    }27    System.out.println();28}
  9. System.out.println();

    26    }27    System.out.println();28}
  10. totalSeats ← 12

    30    int totalSeats→ 12 = rows3 * cols4;31    System.out.println("Total seats: " + totalSeats12);32}
    outputTotal seats: 12
  1. rows ← 5, cols ← 4, seatNumber ← 1

    1public class CreateGrid {2    public static void main(String[] args) {3        // Theater seating chart: rows x seats per row4        int rows→ 5 = 5;5        int cols→ 4 = 4;6        7        // Create 2D array for seat numbers8        int[][] seating = new int[rows][cols];9        10        // Assign seat numbers (1, 2, 3, ...)11        int seatNumber→ 1 = 1;12        for (int row = 0; row < rows; row++) {
  2. for (int row = 0; row < rows; row++)

    pass 1 of 5
    11int seatNumber = 1;12for (int row0 = 0; row < rows5; row++) {13    for (int col = 0; col < cols; col++) {
    All 5 passes — pass 1 is the card above
    passrow
    10
    21
    32
    43
    54
  3. seating[row][col] ← 1, seatNumber ← 2

    pass 1 of 20
    12for (int row = 0; row < rows; row++) {13    for (int col0 = 0; col < cols4; col++) {14        seating[row][col]→ 1 = seatNumber1;15        seatNumber→ 2++;16    }
    20 passes — pass 1 is the card above
    passcolrowseating[row][col]seatNumber
    1000 11 2
    2100 22 3
    3200 33 4
    4300 44 5
    5010 55 6
    6110 66 7
    7210 77 8
    8310 88 9
    9020 99 10
    ⋯ 9 more passes ⋯
    19240 1919 20
    20340 2020 21
  4. System.out.println("=== Theater Seating Chart ===");

    19// Display the seating chart20System.out.println("=== Theater Seating Chart ===");21for (int row = 0; row < rows; row++) {
    output=== Theater Seating Chart ===
  5. for (int row = 0; row < rows; row++)

    pass 1 of 5
    20System.out.println("=== Theater Seating Chart ===");21for (int row0 = 0; row < rows5; row++) {22    System.out.print("Row " + (row0 + 1) + ": ");23    for (int col = 0; col < cols; col++) {
    outputRow 1: 
    All 5 passes — pass 1 is the card above
    passrow
    10
    21
    32
    43
    54
  6. for (int col = 0; col < cols; col++)

    pass 1 of 20
    22System.out.print("Row " + (row + 1) + ": ");23for (int col0 = 0; col < cols4; col++) {24    System.out.print(seating[row][col]1 + "\t");25}
    output1	
    20 passes — pass 1 is the card above
    passcolseating[row][col]row
    1010
    2120
    3230
    4340
    5051
    6161
    7271
    8381
    9092
    ⋯ 9 more passes ⋯
    192194
    203204
  7. System.out.println();

    25    }26    System.out.println();27}
  8. System.out.println();

    25    }26    System.out.println();27}
  9. System.out.println();

    25    }26    System.out.println();27}
  10. System.out.println();

    25    }26    System.out.println();27}
  11. System.out.println();

    25    }26    System.out.println();27}
  12. totalSeats ← 20

    29    int totalSeats→ 20 = rows5 * cols4;30    System.out.println("Total seats: " + totalSeats20);31}
    outputTotal seats: 20
  1. rows ← 3, cols ← 6, seatNumber ← 1

    1public class CreateGrid {2    public static void main(String[] args) {3        // Theater seating chart: rows x seats per row4        int rows→ 3 = 3;5        int cols→ 6 = 6;6        7        // Create 2D array for seat numbers8        int[][] seating = new int[rows][cols];9        10        // Assign seat numbers (1, 2, 3, ...)11        int seatNumber→ 1 = 1;12        for (int row = 0; row < rows; row++) {
  2. for (int row = 0; row < rows; row++)

    pass 1 of 3
    11int seatNumber = 1;12for (int row0 = 0; row < rows3; row++) {13    for (int col = 0; col < cols; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. seating[row][col] ← 1, seatNumber ← 2

    pass 1 of 18
    12for (int row = 0; row < rows; row++) {13    for (int col0 = 0; col < cols6; col++) {14        seating[row][col]→ 1 = seatNumber1;15        seatNumber→ 2++;16    }
    18 passes — pass 1 is the card above
    passcolrowseating[row][col]seatNumber
    1000 11 2
    2100 22 3
    3200 33 4
    4300 44 5
    5400 55 6
    6500 66 7
    7010 77 8
    8110 88 9
    9210 99 10
    ⋯ 7 more passes ⋯
    17420 1717 18
    18520 1818 19
  4. System.out.println("=== Theater Seating Chart ===");

    19// Display the seating chart20System.out.println("=== Theater Seating Chart ===");21for (int row = 0; row < rows; row++) {
    output=== Theater Seating Chart ===
  5. for (int row = 0; row < rows; row++)

    pass 1 of 3
    20System.out.println("=== Theater Seating Chart ===");21for (int row0 = 0; row < rows3; row++) {22    System.out.print("Row " + (row0 + 1) + ": ");23    for (int col = 0; col < cols; col++) {
    outputRow 1: 
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  6. for (int col = 0; col < cols; col++)

    pass 1 of 18
    22System.out.print("Row " + (row + 1) + ": ");23for (int col0 = 0; col < cols6; col++) {24    System.out.print(seating[row][col]1 + "\t");25}
    output1	
    18 passes — pass 1 is the card above
    passcolseating[row][col]row
    1010
    2120
    3230
    4340
    5450
    6560
    7071
    8181
    9291
    ⋯ 7 more passes ⋯
    174172
    185182
  7. System.out.println();

    25    }26    System.out.println();27}
  8. System.out.println();

    25    }26    System.out.println();27}
  9. System.out.println();

    25    }26    System.out.println();27}
  10. totalSeats ← 18

    29    int totalSeats→ 18 = rows3 * cols6;30    System.out.println("Total seats: " + totalSeats18);31}
    outputTotal seats: 18

A 2D array is an "array of arrays". Declare with int[][] grid = new int[rows][cols].

2D array Array of arrays: `int[][] grid`. Access with `grid[row][col]`.

Access a specific seat

Look up a value using row and column indices.

example
AccessElement.java
Replay: real traced execution (multi-file project)
public class AccessElement {
    public static void main(String[] args) {
        // Ticket prices by section (rows) and day (columns)
        // Rows: 0=Front, 1=Middle, 2=Back
        // Cols: 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri
        int[][] prices = {
            {50, 50, 55, 55, 75},   // Front row
            {40, 40, 45, 45, 60},   // Middle row
            {25, 25, 30, 30, 45}    // Back row
        };

        // Customer wants: middle section, Friday
        int wantRow = 1;
        int wantCol = 4;

        int price = prices[wantRow][wantCol];

        String[] sections = {"Front", "Middle", "Back"};
        String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

        System.out.println("=== Ticket Lookup ===");
        System.out.println("Section: " + sections[wantRow]);
        System.out.println("Day: " + days[wantCol]);
        System.out.println("Price: $" + price);

        // Show full price chart
        System.out.println("\n=== Full Price Chart ===");
        System.out.print("        ");
        for (String day : days) {
            System.out.print(day.substring(0, 3) + "\t");
        }
        System.out.println();

        for (int r = 0; r < prices.length; r++) {
            System.out.print(sections[r] + "\t");
            for (int c = 0; c < prices[r].length; c++) {
                System.out.print("$" + prices[r][c] + "\t");
            }
            System.out.println();
        }
    }
}
public class AccessElement {
    public static void main(String[] args) {
        // Ticket prices by section (rows) and day (columns)
        // Rows: 0=Front, 1=Middle, 2=Back
        // Cols: 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri
        int[][] prices = {
            {50, 50, 55, 55, 75},   // Front row
            {40, 40, 45, 45, 60},   // Middle row
            {25, 25, 30, 30, 45}    // Back row
        };

        // Customer wants: middle section, Friday
        int wantRow = 0;
        int wantCol = 4;

        int price = prices[wantRow][wantCol];

        String[] sections = {"Front", "Middle", "Back"};
        String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

        System.out.println("=== Ticket Lookup ===");
        System.out.println("Section: " + sections[wantRow]);
        System.out.println("Day: " + days[wantCol]);
        System.out.println("Price: $" + price);

        // Show full price chart
        System.out.println("\n=== Full Price Chart ===");
        System.out.print("        ");
        for (String day : days) {
            System.out.print(day.substring(0, 3) + "\t");
        }
        System.out.println();

        for (int r = 0; r < prices.length; r++) {
            System.out.print(sections[r] + "\t");
            for (int c = 0; c < prices[r].length; c++) {
                System.out.print("$" + prices[r][c] + "\t");
            }
            System.out.println();
        }
    }
}
public class AccessElement {
    public static void main(String[] args) {
        // Ticket prices by section (rows) and day (columns)
        // Rows: 0=Front, 1=Middle, 2=Back
        // Cols: 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri
        int[][] prices = {
            {50, 50, 55, 55, 75},   // Front row
            {40, 40, 45, 45, 60},   // Middle row
            {25, 25, 30, 30, 45}    // Back row
        };

        // Customer wants: middle section, Friday
        int wantRow = 1;
        int wantCol = 0;

        int price = prices[wantRow][wantCol];

        String[] sections = {"Front", "Middle", "Back"};
        String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

        System.out.println("=== Ticket Lookup ===");
        System.out.println("Section: " + sections[wantRow]);
        System.out.println("Day: " + days[wantCol]);
        System.out.println("Price: $" + price);

        // Show full price chart
        System.out.println("\n=== Full Price Chart ===");
        System.out.print("        ");
        for (String day : days) {
            System.out.print(day.substring(0, 3) + "\t");
        }
        System.out.println();

        for (int r = 0; r < prices.length; r++) {
            System.out.print(sections[r] + "\t");
            for (int c = 0; c < prices[r].length; c++) {
                System.out.print("$" + prices[r][c] + "\t");
            }
            System.out.println();
        }
    }
}
  1. wantRow ← 1, wantCol ← 4, price ← 60

    2public class AccessElement {3    public static void main(String[] args) {4        // Ticket prices by section (rows) and day (columns)5        // Rows: 0=Front, 1=Middle, 2=Back6        // Cols: 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri7        int[][] prices = {8            {50, 50, 55, 55, 75},   // Front row9            {40, 40, 45, 45, 60},   // Middle row  10            {25, 25, 30, 30, 45}    // Back row11        };12        13        // Customer wants: middle section, Friday14        int wantRow→ 1 = 1;  //?rowcol   //@var=_,015        int wantCol→ 4 = 4;              //@var=_,016        17        int price→ 60 = prices[wantRow][wantCol]60;18        19        String[] sections = {"Front", "Middle", "Back"};20        String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};21        22        System.out.println("=== Ticket Lookup ===");23        System.out.println("Section: " + sections[wantRow]Middle);24        System.out.println("Day: " + days[wantCol]Friday);25        System.out.println("Price: $" + price60);26        27        // Show full price chart28        System.out.println("\n=== Full Price Chart ===");29        System.out.print("        ");30        for (String day : days) {
    output=== Ticket Lookup ===
    Section: Middle
    Day: Friday
    Price: $60
    
    === Full Price Chart ===
            
  2. for (String day : days)

    pass 1 of 5
    29System.out.print("        ");30for (String dayMonday : days) {31    System.out.print(day.substring(0, 3) + "\t");32}
    outputMon	
    All 5 passes — pass 1 is the card above
    passday
    1Monday
    2Tuesday
    3Wednesday
    4Thursday
    5Friday
  3. System.out.println();

    32}33System.out.println();
  4. for (int r = 0; r < prices.length; r++)

    pass 1 of 3
    35for (int r0 = 0; r < prices.length3; r++) {36    System.out.print(sections[r]Front + "\t");37    for (int c = 0; c < prices[r].length; c++) {
    outputFront	
    All 3 passes — pass 1 is the card above
    passrsections[r]
    10Front
    21Middle
    32Back
  5. for (int c = 0; c < prices[r].length; c++)

    pass 1 of 15
    36System.out.print(sections[r] + "\t");37for (int c0 = 0; c < prices[r].length5; c++) {38    System.out.print("$" + prices[r][c]50 + "\t");39}
    output$50	
    15 passes — pass 1 is the card above
    passcrprices[r][c]
    10050
    21050
    32055
    43055
    54075
    60140
    71140
    82145
    93145
    ⋯ 4 more passes ⋯
    143230
    154245
  6. System.out.println();

    39    }40    System.out.println();41}
  7. System.out.println();

    39    }40    System.out.println();41}
  8. System.out.println();

    39    }40    System.out.println();41}
  1. wantRow ← 0, wantCol ← 4, price ← 75

    1public class AccessElement {2    public static void main(String[] args) {3        // Ticket prices by section (rows) and day (columns)4        // Rows: 0=Front, 1=Middle, 2=Back5        // Cols: 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri6        int[][] prices = {7            {50, 50, 55, 55, 75},   // Front row8            {40, 40, 45, 45, 60},   // Middle row  9            {25, 25, 30, 30, 45}    // Back row10        };11        12        // Customer wants: middle section, Friday13        int wantRow→ 0 = 0;14        int wantCol→ 4 = 4;15        16        int price→ 75 = prices[wantRow][wantCol]75;17        18        String[] sections = {"Front", "Middle", "Back"};19        String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};20        21        System.out.println("=== Ticket Lookup ===");22        System.out.println("Section: " + sections[wantRow]Front);23        System.out.println("Day: " + days[wantCol]Friday);24        System.out.println("Price: $" + price75);25        26        // Show full price chart27        System.out.println("\n=== Full Price Chart ===");28        System.out.print("        ");29        for (String day : days) {
    output=== Ticket Lookup ===
    Section: Front
    Day: Friday
    Price: $75
    
    === Full Price Chart ===
            
  2. for (String day : days)

    pass 1 of 5
    28System.out.print("        ");29for (String dayMonday : days) {30    System.out.print(day.substring(0, 3) + "\t");31}
    outputMon	
    All 5 passes — pass 1 is the card above
    passday
    1Monday
    2Tuesday
    3Wednesday
    4Thursday
    5Friday
  3. System.out.println();

    31}32System.out.println();
  4. for (int r = 0; r < prices.length; r++)

    pass 1 of 3
    34for (int r0 = 0; r < prices.length3; r++) {35    System.out.print(sections[r]Front + "\t");36    for (int c = 0; c < prices[r].length; c++) {
    outputFront	
    All 3 passes — pass 1 is the card above
    passrsections[r]
    10Front
    21Middle
    32Back
  5. for (int c = 0; c < prices[r].length; c++)

    pass 1 of 15
    35System.out.print(sections[r] + "\t");36for (int c0 = 0; c < prices[r].length5; c++) {37    System.out.print("$" + prices[r][c]50 + "\t");38}
    output$50	
    15 passes — pass 1 is the card above
    passcrprices[r][c]
    10050
    21050
    32055
    43055
    54075
    60140
    71140
    82145
    93145
    ⋯ 4 more passes ⋯
    143230
    154245
  6. System.out.println();

    38    }39    System.out.println();40}
  7. System.out.println();

    38    }39    System.out.println();40}
  8. System.out.println();

    38    }39    System.out.println();40}
  1. wantRow ← 1, wantCol ← 0, price ← 40

    1public class AccessElement {2    public static void main(String[] args) {3        // Ticket prices by section (rows) and day (columns)4        // Rows: 0=Front, 1=Middle, 2=Back5        // Cols: 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri6        int[][] prices = {7            {50, 50, 55, 55, 75},   // Front row8            {40, 40, 45, 45, 60},   // Middle row  9            {25, 25, 30, 30, 45}    // Back row10        };11        12        // Customer wants: middle section, Friday13        int wantRow→ 1 = 1;14        int wantCol→ 0 = 0;15        16        int price→ 40 = prices[wantRow][wantCol]40;17        18        String[] sections = {"Front", "Middle", "Back"};19        String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};20        21        System.out.println("=== Ticket Lookup ===");22        System.out.println("Section: " + sections[wantRow]Middle);23        System.out.println("Day: " + days[wantCol]Monday);24        System.out.println("Price: $" + price40);25        26        // Show full price chart27        System.out.println("\n=== Full Price Chart ===");28        System.out.print("        ");29        for (String day : days) {
    output=== Ticket Lookup ===
    Section: Middle
    Day: Monday
    Price: $40
    
    === Full Price Chart ===
            
  2. for (String day : days)

    pass 1 of 5
    28System.out.print("        ");29for (String dayMonday : days) {30    System.out.print(day.substring(0, 3) + "\t");31}
    outputMon	
    All 5 passes — pass 1 is the card above
    passday
    1Monday
    2Tuesday
    3Wednesday
    4Thursday
    5Friday
  3. System.out.println();

    31}32System.out.println();
  4. for (int r = 0; r < prices.length; r++)

    pass 1 of 3
    34for (int r0 = 0; r < prices.length3; r++) {35    System.out.print(sections[r]Front + "\t");36    for (int c = 0; c < prices[r].length; c++) {
    outputFront	
    All 3 passes — pass 1 is the card above
    passrsections[r]
    10Front
    21Middle
    32Back
  5. for (int c = 0; c < prices[r].length; c++)

    pass 1 of 15
    35System.out.print(sections[r] + "\t");36for (int c0 = 0; c < prices[r].length5; c++) {37    System.out.print("$" + prices[r][c]50 + "\t");38}
    output$50	
    15 passes — pass 1 is the card above
    passcrprices[r][c]
    10050
    21050
    32055
    43055
    54075
    60140
    71140
    82145
    93145
    ⋯ 4 more passes ⋯
    143230
    154245
  6. System.out.println();

    38    }39    System.out.println();40}
  7. System.out.println();

    38    }39    System.out.println();40}
  8. System.out.println();

    38    }39    System.out.println();40}

Access with two indices: grid[1][2] means row 1, column 2.

Mark a seat as reserved

Update a value at a specific position.

example
UpdateCell.java
Replay: real traced execution (multi-file project)
public class UpdateCell {
    public static void main(String[] args) {
        // Seating chart: 0 = available, 1 = reserved
        int[][] seats = {
            {0, 0, 0, 0},
            {0, 0, 0, 0},
            {0, 0, 0, 0}
        };

        // Customer reservations (row, col)
        int reserveRow = 1;
        int reserveCol = 2;

        System.out.println("=== Before Reservation ===");
        printSeating(seats);

        // Make the reservation
        seats[reserveRow][reserveCol] = 1;

        System.out.println("\n=== After Reserving Row " +
            (reserveRow + 1) + ", Seat " + (reserveCol + 1) + " ===");
        printSeating(seats);

        // Reserve more seats
        seats[0][0] = 1;
        seats[2][3] = 1;

        System.out.println("\n=== After Additional Reservations ===");
        printSeating(seats);

        // Count available seats
        int available = 0;
        for (int[] row : seats) {
            for (int seat : row) {
                if (seat == 0) available++;
            }
        }
        System.out.println("\nAvailable seats: " + available);
    }

    static void printSeating(int[][] seats) {
        System.out.println("  1 2 3 4");
        for (int r = 0; r < seats.length; r++) {
            System.out.print((r + 1) + " ");
            for (int c = 0; c < seats[r].length; c++) {
                System.out.print(seats[r][c] == 0 ? "O " : "X ");
            }
            System.out.println();
        }
    }
}
public class UpdateCell {
    public static void main(String[] args) {
        // Seating chart: 0 = available, 1 = reserved
        int[][] seats = {
            {0, 0, 0, 0},
            {0, 0, 0, 0},
            {0, 0, 0, 0}
        };

        // Customer reservations (row, col)
        int reserveRow = 0;
        int reserveCol = 2;

        System.out.println("=== Before Reservation ===");
        printSeating(seats);

        // Make the reservation
        seats[reserveRow][reserveCol] = 1;

        System.out.println("\n=== After Reserving Row " +
            (reserveRow + 1) + ", Seat " + (reserveCol + 1) + " ===");
        printSeating(seats);

        // Reserve more seats
        seats[0][0] = 1;
        seats[2][3] = 1;

        System.out.println("\n=== After Additional Reservations ===");
        printSeating(seats);

        // Count available seats
        int available = 0;
        for (int[] row : seats) {
            for (int seat : row) {
                if (seat == 0) available++;
            }
        }
        System.out.println("\nAvailable seats: " + available);
    }

    static void printSeating(int[][] seats) {
        System.out.println("  1 2 3 4");
        for (int r = 0; r < seats.length; r++) {
            System.out.print((r + 1) + " ");
            for (int c = 0; c < seats[r].length; c++) {
                System.out.print(seats[r][c] == 0 ? "O " : "X ");
            }
            System.out.println();
        }
    }
}
public class UpdateCell {
    public static void main(String[] args) {
        // Seating chart: 0 = available, 1 = reserved
        int[][] seats = {
            {0, 0, 0, 0},
            {0, 0, 0, 0},
            {0, 0, 0, 0}
        };

        // Customer reservations (row, col)
        int reserveRow = 1;
        int reserveCol = 1;

        System.out.println("=== Before Reservation ===");
        printSeating(seats);

        // Make the reservation
        seats[reserveRow][reserveCol] = 1;

        System.out.println("\n=== After Reserving Row " +
            (reserveRow + 1) + ", Seat " + (reserveCol + 1) + " ===");
        printSeating(seats);

        // Reserve more seats
        seats[0][0] = 1;
        seats[2][3] = 1;

        System.out.println("\n=== After Additional Reservations ===");
        printSeating(seats);

        // Count available seats
        int available = 0;
        for (int[] row : seats) {
            for (int seat : row) {
                if (seat == 0) available++;
            }
        }
        System.out.println("\nAvailable seats: " + available);
    }

    static void printSeating(int[][] seats) {
        System.out.println("  1 2 3 4");
        for (int r = 0; r < seats.length; r++) {
            System.out.print((r + 1) + " ");
            for (int c = 0; c < seats[r].length; c++) {
                System.out.print(seats[r][c] == 0 ? "O " : "X ");
            }
            System.out.println();
        }
    }
}
  1. reserveRow ← 1, reserveCol ← 2

    2public class UpdateCell {3    public static void main(String[] args) {4        // Seating chart: 0 = available, 1 = reserved5        int[][] seats = {6            {0, 0, 0, 0},7            {0, 0, 0, 0},8            {0, 0, 0, 0}9        };10        11        // Customer reservations (row, col)12        int reserveRow→ 1 = 1;    //@var=_,013        int reserveCol→ 2 = 2;    //@var=_,114        15        System.out.println("=== Before Reservation ===");16        printSeating(seats);
    output=== Before Reservation ===
  2. static void printSeating(int[][] seats)

    pass 1 of 3
    42static void printSeating(int[][] seats) {43    System.out.println("  1 2 3 4");44    for (int r = 0; r < seats.length; r++) {
    output  1 2 3 4
  3. for (int r = 0; r < seats.length; r++)

    pass 1 of 9
    43System.out.println("  1 2 3 4");44for (int r0 = 0; r < seats.length3; r++) {45    System.out.print((r0 + 1) + " ");46    for (int c = 0; c < seats[r].length; c++) {
    output1 
    All 9 passes — pass 1 is the card above
    passr
    10
    21
    32
    40
    51
    62
    70
    81
    92
  4. for (int c = 0; c < seats[r].length; c++)

    pass 1 of 36
    45System.out.print((r + 1) + " ");46for (int c0 = 0; c < seats[r].length4; c++) {47    System.out.print(seats[r][c]0 == 0 ? "O " : "X ");48}
    outputO 
    36 passes — pass 1 is the card above
    passcrseats[r][c]
    1000
    2100
    3200
    4300
    5010
    6110
    7210
    8310
    9020
    ⋯ 25 more passes ⋯
    35220
    36321
  5. System.out.println();

    48    }49    System.out.println();50}
  6. System.out.println();

    48    }49    System.out.println();50}
  7. seats[reserveRow][reserveCol] ← 1

    15    System.out.println("=== Before Reservation ===");16    printSeating(seats);17    18    // Make the reservation19    seats[reserveRow][reserveCol]→ 1 = 1;  //?update20    21    System.out.println("\n=== After Reserving Row " + 22        (reserveRow1 + 1) + ", Seat " + (reserveCol2 + 1) + " ===");23    printSeating(seats);24    25    // Reserve more seats26    seats[0][0] = 1;  //@var=_,!27    seats[2][3] = 1;  //@var=_,!28    29    System.out.println("\n=== After Additional Reservations ===");  //@var=_,!30    printSeating(seats);  //@var=_,!31    32    // Count available seats33    int available = 0;34    for (int[] row : seats) {35        for (int seat : row) {36            if (seat == 0) available++;37        }38    }39    System.out.println("\nAvailable seats: " + available);40}4142static void printSeating(int[][] seats) {43    System.out.println("  1 2 3 4");44    for (int r = 0; r < seats.length; r++) {45        System.out.print((r + 1) + " ");46        for (int c = 0; c < seats[r].length; c++) {47            System.out.print(seats[r][c] == 0 ? "O " : "X ");48        }49        System.out.println();50    }
    output
    === After Reserving Row 2, Seat 3 ===
  8. System.out.println();

    48    }49    System.out.println();50}
  9. System.out.println();

    48    }49    System.out.println();50}
  10. seats[0][0] ← 1, seats[2][3] ← 1

    22        (reserveRow + 1) + ", Seat " + (reserveCol + 1) + " ===");23    printSeating(seats);24    25    // Reserve more seats26    seats[0][0]→ 1 = 1;  //@var=_,!27    seats[2][3]→ 1 = 1;  //@var=_,!28    29    System.out.println("\n=== After Additional Reservations ===");  //@var=_,!30    printSeating(seats);  //@var=_,!31    32    // Count available seats33    int available = 0;34    for (int[] row : seats) {35        for (int seat : row) {36            if (seat == 0) available++;37        }38    }39    System.out.println("\nAvailable seats: " + available);40}4142static void printSeating(int[][] seats) {43    System.out.println("  1 2 3 4");44    for (int r = 0; r < seats.length; r++) {45        System.out.print((r + 1) + " ");46        for (int c = 0; c < seats[r].length; c++) {47            System.out.print(seats[r][c] == 0 ? "O " : "X ");48        }49        System.out.println();50    }
    output
    === After Additional Reservations ===
  11. System.out.println();

    48    }49    System.out.println();50}
  12. System.out.println();

    48    }49    System.out.println();50}
  13. available ← 0

    29    System.out.println("\n=== After Additional Reservations ===");  //@var=_,!30    printSeating(seats);  //@var=_,!31    32    // Count available seats33    int available→ 0 = 0;34    for (int[] row : seats) {35        for (int seat : row) {36            if (seat == 0) available++;37        }38    }39    System.out.println("\nAvailable seats: " + available);40}4142static void printSeating(int[][] seats) {43    System.out.println("  1 2 3 4");44    for (int r = 0; r < seats.length; r++) {45        System.out.print((r + 1) + " ");46        for (int c = 0; c < seats[r].length; c++) {47            System.out.print(seats[r][c] == 0 ? "O " : "X ");48        }49        System.out.println();50    }
  14. for (int[] row : seats)

    pass 1 of 3
    33int available = 0;34for (int[] row : seats) {35    for (int seat : row) {
  15. for (int seat : row)

    pass 1 of 12
    34for (int[] row : seats) {35    for (int seat1 : row) {36        if (seat == 0) available++;
    All 12 passes — pass 1 is the card above
    passseat
    11
    20
    30
    40
    50
    60
    71
    80
    90
    100
    110
    121
  16. available ← 1

    pass 1 of 9
    35for (int seat : row) {36    if (seat0 == 0) available→ 1++;37}
    All 9 passes — pass 1 is the card above
    passavailable
    10 1
    21 2
    32 3
    43 4
    54 5
    65 6
    76 7
    87 8
    98 9
  17. System.out.println(" Available seats: " + available);

    38    }39    System.out.println("\nAvailable seats: " + available9);40}
    output
    Available seats: 9
  1. reserveRow ← 0, reserveCol ← 2

    1public class UpdateCell {2    public static void main(String[] args) {3        // Seating chart: 0 = available, 1 = reserved4        int[][] seats = {5            {0, 0, 0, 0},6            {0, 0, 0, 0},7            {0, 0, 0, 0}8        };9        10        // Customer reservations (row, col)11        int reserveRow→ 0 = 0;12        int reserveCol→ 2 = 2;13        14        System.out.println("=== Before Reservation ===");15        printSeating(seats);
    output=== Before Reservation ===
  2. static void printSeating(int[][] seats)

    pass 1 of 3
    41static void printSeating(int[][] seats) {42    System.out.println("  1 2 3 4");43    for (int r = 0; r < seats.length; r++) {
    output  1 2 3 4
  3. for (int r = 0; r < seats.length; r++)

    pass 1 of 9
    42System.out.println("  1 2 3 4");43for (int r0 = 0; r < seats.length3; r++) {44    System.out.print((r0 + 1) + " ");45    for (int c = 0; c < seats[r].length; c++) {
    output1 
    All 9 passes — pass 1 is the card above
    passr
    10
    21
    32
    40
    51
    62
    70
    81
    92
  4. for (int c = 0; c < seats[r].length; c++)

    pass 1 of 36
    44System.out.print((r + 1) + " ");45for (int c0 = 0; c < seats[r].length4; c++) {46    System.out.print(seats[r][c]0 == 0 ? "O " : "X ");47}
    outputO 
    36 passes — pass 1 is the card above
    passcrseats[r][c]
    1000
    2100
    3200
    4300
    5010
    6110
    7210
    8310
    9020
    ⋯ 25 more passes ⋯
    35220
    36321
  5. System.out.println();

    47    }48    System.out.println();49}
  6. System.out.println();

    47    }48    System.out.println();49}
  7. seats[reserveRow][reserveCol] ← 1

    14    System.out.println("=== Before Reservation ===");15    printSeating(seats);16    17    // Make the reservation18    seats[reserveRow][reserveCol]→ 1 = 1;19    20    System.out.println("\n=== After Reserving Row " + 21        (reserveRow0 + 1) + ", Seat " + (reserveCol2 + 1) + " ===");22    printSeating(seats);23    24    // Reserve more seats25    seats[0][0] = 1;26    seats[2][3] = 1;27    28    System.out.println("\n=== After Additional Reservations ===");29    printSeating(seats);30    31    // Count available seats32    int available = 0;33    for (int[] row : seats) {34        for (int seat : row) {35            if (seat == 0) available++;36        }37    }38    System.out.println("\nAvailable seats: " + available);39}4041static void printSeating(int[][] seats) {42    System.out.println("  1 2 3 4");43    for (int r = 0; r < seats.length; r++) {44        System.out.print((r + 1) + " ");45        for (int c = 0; c < seats[r].length; c++) {46            System.out.print(seats[r][c] == 0 ? "O " : "X ");47        }48        System.out.println();49    }
    output
    === After Reserving Row 1, Seat 3 ===
  8. System.out.println();

    47    }48    System.out.println();49}
  9. System.out.println();

    47    }48    System.out.println();49}
  10. seats[0][0] ← 1, seats[2][3] ← 1

    21        (reserveRow + 1) + ", Seat " + (reserveCol + 1) + " ===");22    printSeating(seats);23    24    // Reserve more seats25    seats[0][0]→ 1 = 1;26    seats[2][3]→ 1 = 1;27    28    System.out.println("\n=== After Additional Reservations ===");29    printSeating(seats);30    31    // Count available seats32    int available = 0;33    for (int[] row : seats) {34        for (int seat : row) {35            if (seat == 0) available++;36        }37    }38    System.out.println("\nAvailable seats: " + available);39}4041static void printSeating(int[][] seats) {42    System.out.println("  1 2 3 4");43    for (int r = 0; r < seats.length; r++) {44        System.out.print((r + 1) + " ");45        for (int c = 0; c < seats[r].length; c++) {46            System.out.print(seats[r][c] == 0 ? "O " : "X ");47        }48        System.out.println();49    }
    output
    === After Additional Reservations ===
  11. System.out.println();

    47    }48    System.out.println();49}
  12. System.out.println();

    47    }48    System.out.println();49}
  13. available ← 0

    28    System.out.println("\n=== After Additional Reservations ===");29    printSeating(seats);30    31    // Count available seats32    int available→ 0 = 0;33    for (int[] row : seats) {34        for (int seat : row) {35            if (seat == 0) available++;36        }37    }38    System.out.println("\nAvailable seats: " + available);39}4041static void printSeating(int[][] seats) {42    System.out.println("  1 2 3 4");43    for (int r = 0; r < seats.length; r++) {44        System.out.print((r + 1) + " ");45        for (int c = 0; c < seats[r].length; c++) {46            System.out.print(seats[r][c] == 0 ? "O " : "X ");47        }48        System.out.println();49    }
  14. for (int[] row : seats)

    pass 1 of 3
    32int available = 0;33for (int[] row : seats) {34    for (int seat : row) {
  15. for (int seat : row)

    pass 1 of 12
    33for (int[] row : seats) {34    for (int seat1 : row) {35        if (seat == 0) available++;
    All 12 passes — pass 1 is the card above
    passseat
    11
    20
    31
    40
    50
    60
    70
    80
    90
    100
    110
    121
  16. available ← 1

    pass 1 of 9
    34for (int seat : row) {35    if (seat0 == 0) available→ 1++;36}
    All 9 passes — pass 1 is the card above
    passavailable
    10 1
    21 2
    32 3
    43 4
    54 5
    65 6
    76 7
    87 8
    98 9
  17. System.out.println(" Available seats: " + available);

    37    }38    System.out.println("\nAvailable seats: " + available9);39}
    output
    Available seats: 9
  1. reserveRow ← 1, reserveCol ← 1

    1public class UpdateCell {2    public static void main(String[] args) {3        // Seating chart: 0 = available, 1 = reserved4        int[][] seats = {5            {0, 0, 0, 0},6            {0, 0, 0, 0},7            {0, 0, 0, 0}8        };9        10        // Customer reservations (row, col)11        int reserveRow→ 1 = 1;12        int reserveCol→ 1 = 1;13        14        System.out.println("=== Before Reservation ===");15        printSeating(seats);
    output=== Before Reservation ===
  2. static void printSeating(int[][] seats)

    pass 1 of 3
    41static void printSeating(int[][] seats) {42    System.out.println("  1 2 3 4");43    for (int r = 0; r < seats.length; r++) {
    output  1 2 3 4
  3. for (int r = 0; r < seats.length; r++)

    pass 1 of 9
    42System.out.println("  1 2 3 4");43for (int r0 = 0; r < seats.length3; r++) {44    System.out.print((r0 + 1) + " ");45    for (int c = 0; c < seats[r].length; c++) {
    output1 
    All 9 passes — pass 1 is the card above
    passr
    10
    21
    32
    40
    51
    62
    70
    81
    92
  4. for (int c = 0; c < seats[r].length; c++)

    pass 1 of 36
    44System.out.print((r + 1) + " ");45for (int c0 = 0; c < seats[r].length4; c++) {46    System.out.print(seats[r][c]0 == 0 ? "O " : "X ");47}
    outputO 
    36 passes — pass 1 is the card above
    passcrseats[r][c]
    1000
    2100
    3200
    4300
    5010
    6110
    7210
    8310
    9020
    ⋯ 25 more passes ⋯
    35220
    36321
  5. System.out.println();

    47    }48    System.out.println();49}
  6. System.out.println();

    47    }48    System.out.println();49}
  7. seats[reserveRow][reserveCol] ← 1

    14    System.out.println("=== Before Reservation ===");15    printSeating(seats);16    17    // Make the reservation18    seats[reserveRow][reserveCol]→ 1 = 1;19    20    System.out.println("\n=== After Reserving Row " + 21        (reserveRow1 + 1) + ", Seat " + (reserveCol1 + 1) + " ===");22    printSeating(seats);23    24    // Reserve more seats25    seats[0][0] = 1;26    seats[2][3] = 1;27    28    System.out.println("\n=== After Additional Reservations ===");29    printSeating(seats);30    31    // Count available seats32    int available = 0;33    for (int[] row : seats) {34        for (int seat : row) {35            if (seat == 0) available++;36        }37    }38    System.out.println("\nAvailable seats: " + available);39}4041static void printSeating(int[][] seats) {42    System.out.println("  1 2 3 4");43    for (int r = 0; r < seats.length; r++) {44        System.out.print((r + 1) + " ");45        for (int c = 0; c < seats[r].length; c++) {46            System.out.print(seats[r][c] == 0 ? "O " : "X ");47        }48        System.out.println();49    }
    output
    === After Reserving Row 2, Seat 2 ===
  8. System.out.println();

    47    }48    System.out.println();49}
  9. System.out.println();

    47    }48    System.out.println();49}
  10. seats[0][0] ← 1, seats[2][3] ← 1

    21        (reserveRow + 1) + ", Seat " + (reserveCol + 1) + " ===");22    printSeating(seats);23    24    // Reserve more seats25    seats[0][0]→ 1 = 1;26    seats[2][3]→ 1 = 1;27    28    System.out.println("\n=== After Additional Reservations ===");29    printSeating(seats);30    31    // Count available seats32    int available = 0;33    for (int[] row : seats) {34        for (int seat : row) {35            if (seat == 0) available++;36        }37    }38    System.out.println("\nAvailable seats: " + available);39}4041static void printSeating(int[][] seats) {42    System.out.println("  1 2 3 4");43    for (int r = 0; r < seats.length; r++) {44        System.out.print((r + 1) + " ");45        for (int c = 0; c < seats[r].length; c++) {46            System.out.print(seats[r][c] == 0 ? "O " : "X ");47        }48        System.out.println();49    }
    output
    === After Additional Reservations ===
  11. System.out.println();

    47    }48    System.out.println();49}
  12. System.out.println();

    47    }48    System.out.println();49}
  13. available ← 0

    28    System.out.println("\n=== After Additional Reservations ===");29    printSeating(seats);30    31    // Count available seats32    int available→ 0 = 0;33    for (int[] row : seats) {34        for (int seat : row) {35            if (seat == 0) available++;36        }37    }38    System.out.println("\nAvailable seats: " + available);39}4041static void printSeating(int[][] seats) {42    System.out.println("  1 2 3 4");43    for (int r = 0; r < seats.length; r++) {44        System.out.print((r + 1) + " ");45        for (int c = 0; c < seats[r].length; c++) {46            System.out.print(seats[r][c] == 0 ? "O " : "X ");47        }48        System.out.println();49    }
  14. for (int[] row : seats)

    pass 1 of 3
    32int available = 0;33for (int[] row : seats) {34    for (int seat : row) {
  15. for (int seat : row)

    pass 1 of 12
    33for (int[] row : seats) {34    for (int seat1 : row) {35        if (seat == 0) available++;
    All 12 passes — pass 1 is the card above
    passseat
    11
    20
    30
    40
    50
    61
    70
    80
    90
    100
    110
    121
  16. available ← 1

    pass 1 of 9
    34for (int seat : row) {35    if (seat0 == 0) available→ 1++;36}
    All 9 passes — pass 1 is the card above
    passavailable
    10 1
    21 2
    32 3
    43 4
    54 5
    65 6
    76 7
    87 8
    98 9
  17. System.out.println(" Available seats: " + available);

    37    }38    System.out.println("\nAvailable seats: " + available9);39}
    output
    Available seats: 9

Assign directly: grid[row][col] = value. Arrays are mutable.

Sum all values

Calculate a total by traversing every cell.

data
SumAll.java
Replay: real traced execution (multi-file project)
public class SumAll {
    public static void main(String[] args) {
        // Monthly sales by product (in units)
        // Rows: Products (0=Widgets, 1=Gadgets, 2=Gizmos)
        // Cols: Months (0=Jan, 1=Feb, 2=Mar)
        int[][] sales = {
            {120, 135, 150},  // Widgets
            {80, 95, 110},    // Gadgets
            {200, 180, 220}   // Gizmos
        };

        // Quarterly sales (4 products, 4 quarters)
        int[][] quarterly = {
            {1200, 1350, 1500, 1400},
            {800, 950, 1100, 1050},
            {2000, 1800, 2200, 2100},
            {500, 600, 700, 650}
        };

        int[][] data = sales;

        // Calculate total sales
        int grandTotal = 0;
        for (int row = 0; row < data.length; row++) {
            for (int col = 0; col < data[row].length; col++) {
                grandTotal += data[row][col];
            }
        }

        System.out.println("=== Sales Summary ===");
        System.out.println("Grand Total: " + grandTotal + " units");

        // Calculate row totals (per product)
        System.out.println("\n=== Per Product ===");
        String[] products = {"Widgets", "Gadgets", "Gizmos", "Sprockets"};
        for (int row = 0; row < data.length; row++) {
            int rowTotal = 0;
            for (int col = 0; col < data[row].length; col++) {
                rowTotal += data[row][col];
            }
            System.out.println(products[row] + ": " + rowTotal);
        }

        // Calculate column totals (per month/quarter)
        System.out.println("\n=== Per Period ===");
        for (int col = 0; col < data[0].length; col++) {
            int colTotal = 0;
            for (int row = 0; row < data.length; row++) {
                colTotal += data[row][col];
            }
            System.out.println("Period " + (col + 1) + ": " + colTotal);
        }
    }
}
public class SumAll {
    public static void main(String[] args) {
        // Monthly sales by product (in units)
        // Rows: Products (0=Widgets, 1=Gadgets, 2=Gizmos)
        // Cols: Months (0=Jan, 1=Feb, 2=Mar)
        int[][] sales = {
            {120, 135, 150},  // Widgets
            {80, 95, 110},    // Gadgets
            {200, 180, 220}   // Gizmos
        };

        // Quarterly sales (4 products, 4 quarters)
        int[][] quarterly = {
            {1200, 1350, 1500, 1400},
            {800, 950, 1100, 1050},
            {2000, 1800, 2200, 2100},
            {500, 600, 700, 650}
        };

        int[][] data = quarterly;

        // Calculate total sales
        int grandTotal = 0;
        for (int row = 0; row < data.length; row++) {
            for (int col = 0; col < data[row].length; col++) {
                grandTotal += data[row][col];
            }
        }

        System.out.println("=== Sales Summary ===");
        System.out.println("Grand Total: " + grandTotal + " units");

        // Calculate row totals (per product)
        System.out.println("\n=== Per Product ===");
        String[] products = {"Widgets", "Gadgets", "Gizmos", "Sprockets"};
        for (int row = 0; row < data.length; row++) {
            int rowTotal = 0;
            for (int col = 0; col < data[row].length; col++) {
                rowTotal += data[row][col];
            }
            System.out.println(products[row] + ": " + rowTotal);
        }

        // Calculate column totals (per month/quarter)
        System.out.println("\n=== Per Period ===");
        for (int col = 0; col < data[0].length; col++) {
            int colTotal = 0;
            for (int row = 0; row < data.length; row++) {
                colTotal += data[row][col];
            }
            System.out.println("Period " + (col + 1) + ": " + colTotal);
        }
    }
}
  1. grandTotal ← 0

    2public class SumAll {3    public static void main(String[] args) {4        // Monthly sales by product (in units)5        // Rows: Products (0=Widgets, 1=Gadgets, 2=Gizmos)6        // Cols: Months (0=Jan, 1=Feb, 2=Mar)7        int[][] sales = {     //@var=_,!8            {120, 135, 150},  // Widgets9            {80, 95, 110},    // Gadgets10            {200, 180, 220}   // Gizmos11        };12        13        // Quarterly sales (4 products, 4 quarters)14        int[][] quarterly = {  //@var=!,_15            {1200, 1350, 1500, 1400},16            {800, 950, 1100, 1050},17            {2000, 1800, 2200, 2100},18            {500, 600, 700, 650}19        };20        21        int[][] data = sales;  //@var=_,quarterly22        23        // Calculate total sales24        int grandTotal→ 0 = 0;  //?sum25        for (int row = 0; row < data.length; row++) {
  2. for (int row = 0; row < data.length; row++)

    pass 1 of 3
    24int grandTotal = 0;  //?sum25for (int row0 = 0; row < data.length3; row++) {26    for (int col = 0; col < data[row].length; col++) {
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. grandTotal ← 120

    pass 1 of 9
    25for (int row = 0; row < data.length; row++) {26    for (int col0 = 0; col < data[row].length3; col++) {27        grandTotal→ 120 += data[row][col]120;28    }
    All 9 passes — pass 1 is the card above
    passcolrowdata[row][col]grandTotal
    1001200 120
    210135120 255
    320150255 405
    40180405 485
    51195485 580
    621110580 690
    702200690 890
    812180890 1070
    9222201070 1290
  4. System.out.println("Grand Total: " + grandTotal + " units");

    31System.out.println("=== Sales Summary ===");32System.out.println("Grand Total: " + grandTotal1290 + " units");3334// Calculate row totals (per product)35System.out.println("\n=== Per Product ===");36String[] products = {"Widgets", "Gadgets", "Gizmos", "Sprockets"};37for (int row = 0; row < data.length; row++) {
    output=== Sales Summary ===
    Grand Total: 1290 units
    
    === Per Product ===
  5. rowTotal ← 0

    pass 1 of 3
    36String[] products = {"Widgets", "Gadgets", "Gizmos", "Sprockets"};37for (int row0 = 0; row < data.length3; row++) {38    int rowTotal→ 0 = 0;39    for (int col = 0; col < data[row].length; col++) {
    All 3 passes — pass 1 is the card above
    passrowrowTotal
    100
    210
    320
  6. rowTotal ← 120

    pass 1 of 9
    38int rowTotal = 0;39for (int col0 = 0; col < data[row].length3; col++) {40    rowTotal→ 120 += data[row][col]120;41}
    All 9 passes — pass 1 is the card above
    passcolrowdata[row][col]rowTotal
    1001200 120
    210135120 255
    320150255 405
    401800 80
    5119580 175
    621110175 285
    7022000 200
    812180200 380
    922220380 600
  7. System.out.println(products[row] + ": " + rowTotal);

    41    }42    System.out.println(products[row]Widgets + ": " + rowTotal405);43}
    outputWidgets: 405
    values this step0row
  8. System.out.println(products[row] + ": " + rowTotal);

    41    }42    System.out.println(products[row]Gadgets + ": " + rowTotal285);43}
    outputGadgets: 285
    values this step1row
  9. System.out.println(products[row] + ": " + rowTotal);

    41    }42    System.out.println(products[row]Gizmos + ": " + rowTotal600);43}
    outputGizmos: 600
    values this step2row
  10. System.out.println(" === Per Period ===");

    45// Calculate column totals (per month/quarter)46System.out.println("\n=== Per Period ===");47for (int col = 0; col < data[0].length; col++) {
    output
    === Per Period ===
  11. colTotal ← 0

    pass 1 of 3
    46System.out.println("\n=== Per Period ===");47for (int col0 = 0; col < data[0].length3; col++) {48    int colTotal→ 0 = 0;49    for (int row = 0; row < data.length; row++) {
    All 3 passes — pass 1 is the card above
    passcolcolTotal
    100
    210
    320
  12. colTotal ← 120

    pass 1 of 9
    48int colTotal = 0;49for (int row0 = 0; row < data.length3; row++) {50    colTotal→ 120 += data[row][col]120;51}
    All 9 passes — pass 1 is the card above
    passrowdata[row][col]colcolTotal
    1012000 120
    21800120 200
    322000200 400
    4013510 135
    51951135 230
    621801230 410
    7015020 150
    811102150 260
    922202260 480
  13. System.out.println("Period " + (col + 1) + ": " + colTotal);

    51    }52    System.out.println("Period " + (col0 + 1) + ": " + colTotal400);53}
    outputPeriod 1: 400
  14. System.out.println("Period " + (col + 1) + ": " + colTotal);

    51    }52    System.out.println("Period " + (col1 + 1) + ": " + colTotal410);53}
    outputPeriod 2: 410
  15. System.out.println("Period " + (col + 1) + ": " + colTotal);

    51    }52    System.out.println("Period " + (col2 + 1) + ": " + colTotal480);53}
    outputPeriod 3: 480
  1. grandTotal ← 0

    1public class SumAll {2    public static void main(String[] args) {3        // Monthly sales by product (in units)4        // Rows: Products (0=Widgets, 1=Gadgets, 2=Gizmos)5        // Cols: Months (0=Jan, 1=Feb, 2=Mar)6        int[][] sales = {7            {120, 135, 150},  // Widgets8            {80, 95, 110},    // Gadgets9            {200, 180, 220}   // Gizmos10        };11        12        // Quarterly sales (4 products, 4 quarters)13        int[][] quarterly = {14            {1200, 1350, 1500, 1400},15            {800, 950, 1100, 1050},16            {2000, 1800, 2200, 2100},17            {500, 600, 700, 650}18        };19        20        int[][] data = quarterly;21        22        // Calculate total sales23        int grandTotal→ 0 = 0;24        for (int row = 0; row < data.length; row++) {
  2. for (int row = 0; row < data.length; row++)

    pass 1 of 4
    23int grandTotal = 0;24for (int row0 = 0; row < data.length4; row++) {25    for (int col = 0; col < data[row].length; col++) {
    All 4 passes — pass 1 is the card above
    passrow
    10
    21
    32
    43
  3. grandTotal ← 1200

    pass 1 of 16
    24for (int row = 0; row < data.length; row++) {25    for (int col0 = 0; col < data[row].length4; col++) {26        grandTotal→ 1200 += data[row][col]1200;27    }
    16 passes — pass 1 is the card above
    passcolrowdata[row][col]grandTotal
    10012000 1200
    21013501200 2550
    32015002550 4050
    43014004050 5450
    5018005450 6250
    6119506250 7200
    72111007200 8300
    83110508300 9350
    90220009350 11350
    ⋯ 5 more passes ⋯
    152370018550 19250
    163365019250 19900
  4. System.out.println("Grand Total: " + grandTotal + " units");

    30System.out.println("=== Sales Summary ===");31System.out.println("Grand Total: " + grandTotal19900 + " units");3233// Calculate row totals (per product)34System.out.println("\n=== Per Product ===");35String[] products = {"Widgets", "Gadgets", "Gizmos", "Sprockets"};36for (int row = 0; row < data.length; row++) {
    output=== Sales Summary ===
    Grand Total: 19900 units
    
    === Per Product ===
  5. rowTotal ← 0

    pass 1 of 4
    35String[] products = {"Widgets", "Gadgets", "Gizmos", "Sprockets"};36for (int row0 = 0; row < data.length4; row++) {37    int rowTotal→ 0 = 0;38    for (int col = 0; col < data[row].length; col++) {
    All 4 passes — pass 1 is the card above
    passrowrowTotal
    100
    210
    320
    430
  6. rowTotal ← 1200

    pass 1 of 16
    37int rowTotal = 0;38for (int col0 = 0; col < data[row].length4; col++) {39    rowTotal→ 1200 += data[row][col]1200;40}
    16 passes — pass 1 is the card above
    passcolrowdata[row][col]rowTotal
    10012000 1200
    21013501200 2550
    32015002550 4050
    43014004050 5450
    5018000 800
    611950800 1750
    72111001750 2850
    83110502850 3900
    90220000 2000
    ⋯ 5 more passes ⋯
    15237001100 1800
    16336501800 2450
  7. System.out.println(products[row] + ": " + rowTotal);

    40    }41    System.out.println(products[row]Widgets + ": " + rowTotal5450);42}
    outputWidgets: 5450
    values this step0row
  8. System.out.println(products[row] + ": " + rowTotal);

    40    }41    System.out.println(products[row]Gadgets + ": " + rowTotal3900);42}
    outputGadgets: 3900
    values this step1row
  9. System.out.println(products[row] + ": " + rowTotal);

    40    }41    System.out.println(products[row]Gizmos + ": " + rowTotal8100);42}
    outputGizmos: 8100
    values this step2row
  10. System.out.println(products[row] + ": " + rowTotal);

    40    }41    System.out.println(products[row]Sprockets + ": " + rowTotal2450);42}
    outputSprockets: 2450
    values this step3row
  11. System.out.println(" === Per Period ===");

    44// Calculate column totals (per month/quarter)45System.out.println("\n=== Per Period ===");46for (int col = 0; col < data[0].length; col++) {
    output
    === Per Period ===
  12. colTotal ← 0

    pass 1 of 4
    45System.out.println("\n=== Per Period ===");46for (int col0 = 0; col < data[0].length4; col++) {47    int colTotal→ 0 = 0;48    for (int row = 0; row < data.length; row++) {
    All 4 passes — pass 1 is the card above
    passcolcolTotal
    100
    210
    320
    430
  13. colTotal ← 1200

    pass 1 of 16
    47int colTotal = 0;48for (int row0 = 0; row < data.length4; row++) {49    colTotal→ 1200 += data[row][col]1200;50}
    16 passes — pass 1 is the card above
    passrowdata[row][col]colcolTotal
    10120000 1200
    2180001200 2000
    32200002000 4000
    4350004000 4500
    50135010 1350
    6195011350 2300
    72180012300 4100
    8360014100 4700
    90150020 1500
    ⋯ 5 more passes ⋯
    152210032450 4550
    16365034550 5200
  14. System.out.println("Period " + (col + 1) + ": " + colTotal);

    50    }51    System.out.println("Period " + (col0 + 1) + ": " + colTotal4500);52}
    outputPeriod 1: 4500
  15. System.out.println("Period " + (col + 1) + ": " + colTotal);

    50    }51    System.out.println("Period " + (col1 + 1) + ": " + colTotal4700);52}
    outputPeriod 2: 4700
  16. System.out.println("Period " + (col + 1) + ": " + colTotal);

    50    }51    System.out.println("Period " + (col2 + 1) + ": " + colTotal5500);52}
    outputPeriod 3: 5500
  17. System.out.println("Period " + (col + 1) + ": " + colTotal);

    50    }51    System.out.println("Period " + (col3 + 1) + ": " + colTotal5200);52}
    outputPeriod 4: 5200

Use nested loops: outer loop for rows, inner loop for columns.

row-major Process row by row: `for (row) { for (col) { grid[row][col] } }`

Find a value's position

Search for a value and return its row/column.

targetSeat
FindPosition.java
Replay: real traced execution (multi-file project)
public class FindPosition {
    public static void main(String[] args) {
        // Movie theater seat map with seat IDs
        String[][] seatMap = {
            {"A1", "A2", "A3", "A4", "A5"},
            {"B1", "B2", "B3", "B4", "B5"},
            {"C1", "C2", "C3", "C4", "C5"},
            {"D1", "D2", "D3", "D4", "D5"}
        };

        // Find this seat
        String targetSeat = "C3";

        // Search for the seat
        int foundRow = -1;
        int foundCol = -1;

        for (int row = 0; row < seatMap.length; row++) {
            for (int col = 0; col < seatMap[row].length; col++) {
                if (seatMap[row][col].equals(targetSeat)) {
                    foundRow = row;
                    foundCol = col;
                    break;  // Found it, exit inner loop
                }
            }
            if (foundRow != -1) break;  // Exit outer loop too
        }

        System.out.println("=== Seat Finder ===");
        System.out.println("Looking for seat: " + targetSeat);

        if (foundRow != -1) {
            System.out.println("Found at position:");
            System.out.println("  Row index: " + foundRow + " (Row " + (foundRow + 1) + ")");
            System.out.println("  Col index: " + foundCol + " (Seat " + (foundCol + 1) + ")");

            // Calculate walking directions
            System.out.println("\nDirections: Walk to row " + (foundRow + 1) +
                ", then count " + (foundCol + 1) + " seat(s) from the left.");
        } else {
            System.out.println("Seat not found!");
        }

        // Show the map with target highlighted
        System.out.println("\n=== Seat Map ===");
        for (int row = 0; row < seatMap.length; row++) {
            for (int col = 0; col < seatMap[row].length; col++) {
                if (row == foundRow && col == foundCol) {
                    System.out.print("[" + seatMap[row][col] + "]\t");
                } else {
                    System.out.print(" " + seatMap[row][col] + " \t");
                }
            }
            System.out.println();
        }
    }
}
public class FindPosition {
    public static void main(String[] args) {
        // Movie theater seat map with seat IDs
        String[][] seatMap = {
            {"A1", "A2", "A3", "A4", "A5"},
            {"B1", "B2", "B3", "B4", "B5"},
            {"C1", "C2", "C3", "C4", "C5"},
            {"D1", "D2", "D3", "D4", "D5"}
        };

        // Find this seat
        String targetSeat = "Z9";

        // Search for the seat
        int foundRow = -1;
        int foundCol = -1;

        for (int row = 0; row < seatMap.length; row++) {
            for (int col = 0; col < seatMap[row].length; col++) {
                if (seatMap[row][col].equals(targetSeat)) {
                    foundRow = row;
                    foundCol = col;
                    break;  // Found it, exit inner loop
                }
            }
            if (foundRow != -1) break;  // Exit outer loop too
        }

        System.out.println("=== Seat Finder ===");
        System.out.println("Looking for seat: " + targetSeat);

        if (foundRow != -1) {
            System.out.println("Found at position:");
            System.out.println("  Row index: " + foundRow + " (Row " + (foundRow + 1) + ")");
            System.out.println("  Col index: " + foundCol + " (Seat " + (foundCol + 1) + ")");

            // Calculate walking directions
            System.out.println("\nDirections: Walk to row " + (foundRow + 1) +
                ", then count " + (foundCol + 1) + " seat(s) from the left.");
        } else {
            System.out.println("Seat not found!");
        }

        // Show the map with target highlighted
        System.out.println("\n=== Seat Map ===");
        for (int row = 0; row < seatMap.length; row++) {
            for (int col = 0; col < seatMap[row].length; col++) {
                if (row == foundRow && col == foundCol) {
                    System.out.print("[" + seatMap[row][col] + "]\t");
                } else {
                    System.out.print(" " + seatMap[row][col] + " \t");
                }
            }
            System.out.println();
        }
    }
}
  1. targetSeat ← C3, foundRow ← -1, foundCol ← -1

    2public class FindPosition {3    public static void main(String[] args) {4        // Movie theater seat map with seat IDs5        String[][] seatMap = {6            {"A1", "A2", "A3", "A4", "A5"},7            {"B1", "B2", "B3", "B4", "B5"},8            {"C1", "C2", "C3", "C4", "C5"},9            {"D1", "D2", "D3", "D4", "D5"}10        };11        12        // Find this seat13        String targetSeat→ C3 = "C3";  //@var=_,Z914        15        // Search for the seat16        int foundRow→ -1 = -1;  //?found17        int foundCol→ -1 = -1;
  2. for (int row = 0; row < seatMap.length; row++)

    pass 1 of 3
    19for (int row0 = 0; row < seatMap.length4; row++) {20    for (int col = 0; col < seatMap[row].length; col++) {
    All 3 passes — pass 1 is the card above
    passrowseatMap[row][col]coltargetSeatfoundRowfoundCol
    10
    21
    32C32C322
  3. for (int col = 0; col < seatMap[row].length; col++)

    pass 1 of 13
    19for (int row = 0; row < seatMap.length; row++) {20    for (int col0 = 0; col < seatMap[row].length5; col++) {21        if (seatMap[row][col].equals(targetSeat)) {
    13 passes — pass 1 is the card above
    passcolrowseatMap[row][col]targetSeatfoundRowfoundCol
    100
    210
    320
    430
    540
    601
    711
    821
    931
    ⋯ 2 more passes ⋯
    1212
    1322C3C322
  4. foundRow ← 2, foundCol ← 2

    20for (int col = 0; col < seatMap[row].length; col++) {21    if (seatMap[row][col]C3.equals(targetSeatC3)) {22        foundRow→ 2 = row2;23        foundCol→ 2 = col2;24        break;  // Found it, exit inner loop25    }
  5. if (foundRow != -1)

    26    }27    if (foundRow2 != -1) break;  // Exit outer loop too28}
  6. System.out.println("Looking for seat: " + targetSeat);

    30System.out.println("=== Seat Finder ===");31System.out.println("Looking for seat: " + targetSeatC3);
    output=== Seat Finder ===
    Looking for seat: C3
  7. if (foundRow != -1)

    33if (foundRow2 != -1) {34    System.out.println("Found at position:");35    System.out.println("  Row index: " + foundRow2 + " (Row " + (foundRow + 1) + ")");36    System.out.println("  Col index: " + foundCol2 + " (Seat " + (foundCol + 1) + ")");37    38    // Calculate walking directions39    System.out.println("\nDirections: Walk to row " + (foundRow2 + 1) + 40        ", then count " + (foundCol2 + 1) + " seat(s) from the left.");41} else {
    outputFound at position:
      Row index: 2 (Row 3)
      Col index: 2 (Seat 3)
    
    Directions: Walk to row 3, then count 3 seat(s) from the left.
  8. System.out.println(" === Seat Map ===");

    45// Show the map with target highlighted46System.out.println("\n=== Seat Map ===");47for (int row = 0; row < seatMap.length; row++) {
    output
    === Seat Map ===
  9. for (int row = 0; row < seatMap.length; row++)

    pass 1 of 4
    46System.out.println("\n=== Seat Map ===");47for (int row0 = 0; row < seatMap.length4; row++) {48    for (int col = 0; col < seatMap[row].length; col++) {
    All 4 passes — pass 1 is the card above
    passrowfoundRowcolfoundColseatMap[row][col]
    10
    21
    32222C3
    43
  10. for (int col = 0; col < seatMap[row].length; col++)

    pass 1 of 20
    47for (int row = 0; row < seatMap.length; row++) {48    for (int col0 = 0; col < seatMap[row].length5; col++) {49        if (row == foundRow && col == foundCol) {
    20 passes — pass 1 is the card above
    passcolrowfoundRowfoundColseatMap[row][col]
    100
    210
    320
    430
    540
    601
    711
    821
    931
    ⋯ 9 more passes ⋯
    1933
    2043
  11. else

    pass 1 of 19
    50    System.out.print("[" + seatMap[row][col] + "]\t");51} else {52    System.out.print(" " + seatMap[row][col]A1 + " \t");53}
    output A1 	
    19 passes — pass 1 is the card above
    passseatMap[row][col]rowcolfoundRowfoundCol
    1A100
    2A201
    3A302
    4A403
    5A504
    6B110
    7B211
    8B312
    9B413
    ⋯ 8 more passes ⋯
    18D433
    19D534
  12. System.out.println();

    54    }55    System.out.println();56}
  13. System.out.println();

    54    }55    System.out.println();56}
  14. if (row == foundRow && col == foundCol)

    48for (int col = 0; col < seatMap[row].length; col++) {49    if (row2 == foundRow2 && col2 == foundCol2) {50        System.out.print("[" + seatMap[row][col]C3 + "]\t");51    } else {
    output[C3]	
  15. System.out.println();

    54    }55    System.out.println();56}
  16. System.out.println();

    54    }55    System.out.println();56}
  1. targetSeat ← Z9, foundRow ← -1, foundCol ← -1

    1public class FindPosition {2    public static void main(String[] args) {3        // Movie theater seat map with seat IDs4        String[][] seatMap = {5            {"A1", "A2", "A3", "A4", "A5"},6            {"B1", "B2", "B3", "B4", "B5"},7            {"C1", "C2", "C3", "C4", "C5"},8            {"D1", "D2", "D3", "D4", "D5"}9        };10        11        // Find this seat12        String targetSeat→ Z9 = "Z9";13        14        // Search for the seat15        int foundRow→ -1 = -1;16        int foundCol→ -1 = -1;
  2. for (int row = 0; row < seatMap.length; row++)

    pass 1 of 4
    18for (int row0 = 0; row < seatMap.length4; row++) {19    for (int col = 0; col < seatMap[row].length; col++) {
    All 4 passes — pass 1 is the card above
    passrow
    10
    21
    32
    43
  3. for (int col = 0; col < seatMap[row].length; col++)

    pass 1 of 20
    18for (int row = 0; row < seatMap.length; row++) {19    for (int col0 = 0; col < seatMap[row].length5; col++) {20        if (seatMap[row][col].equals(targetSeat)) {
    20 passes — pass 1 is the card above
    passcolrow
    100
    210
    320
    430
    540
    601
    711
    821
    931
    ⋯ 9 more passes ⋯
    1933
    2043
  4. System.out.println("Looking for seat: " + targetSeat);

    29System.out.println("=== Seat Finder ===");30System.out.println("Looking for seat: " + targetSeatZ9);
    output=== Seat Finder ===
    Looking for seat: Z9
  5. else

    39        ", then count " + (foundCol + 1) + " seat(s) from the left.");40} else {41    System.out.println("Seat not found!");42}
    outputSeat not found!
  6. System.out.println(" === Seat Map ===");

    44// Show the map with target highlighted45System.out.println("\n=== Seat Map ===");46for (int row = 0; row < seatMap.length; row++) {
    output
    === Seat Map ===
  7. for (int row = 0; row < seatMap.length; row++)

    pass 1 of 4
    45System.out.println("\n=== Seat Map ===");46for (int row0 = 0; row < seatMap.length4; row++) {47    for (int col = 0; col < seatMap[row].length; col++) {
    All 4 passes — pass 1 is the card above
    passrow
    10
    21
    32
    43
  8. for (int col = 0; col < seatMap[row].length; col++)

    pass 1 of 20
    46for (int row = 0; row < seatMap.length; row++) {47    for (int col0 = 0; col < seatMap[row].length5; col++) {48        if (row == foundRow && col == foundCol) {
    20 passes — pass 1 is the card above
    passcolrow
    100
    210
    320
    430
    540
    601
    711
    821
    931
    ⋯ 9 more passes ⋯
    1933
    2043
  9. else

    pass 1 of 20
    49    System.out.print("[" + seatMap[row][col] + "]\t");50} else {51    System.out.print(" " + seatMap[row][col]A1 + " \t");52}
    output A1 	
    20 passes — pass 1 is the card above
    passseatMap[row][col]rowcol
    1A100
    2A201
    3A302
    4A403
    5A504
    6B110
    7B211
    8B312
    9B413
    ⋯ 9 more passes ⋯
    19D433
    20D534
  10. System.out.println();

    53    }54    System.out.println();55}
  11. System.out.println();

    53    }54    System.out.println();55}
  12. System.out.println();

    53    }54    System.out.println();55}
  13. System.out.println();

    53    }54    System.out.println();55}

Return early when found. Return special value (-1, -1) if not found.

Exercise: JaggedArray.java

Explore jagged arrays where rows have different lengths