Arrays & Collections
2D Arrays
Grids and Tables
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.
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);
}
}
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++) {for (int row = 0; row < rows; row++)
pass 1 of 312int 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 pass row1 0 2 1 3 2 seating[row][col] ← 1, seatNumber ← 2
pass 1 of 1213for (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 pass colrowseating[row][col]seatNumber1 0 0 0 → 1 1 → 2 2 1 0 0 → 2 2 → 3 3 2 0 0 → 3 3 → 4 4 3 0 0 → 4 4 → 5 5 0 1 0 → 5 5 → 6 6 1 1 0 → 6 6 → 7 7 2 1 0 → 7 7 → 8 8 3 1 0 → 8 8 → 9 9 0 2 0 → 9 9 → 10 10 1 2 0 → 10 10 → 11 11 2 2 0 → 11 11 → 12 12 3 2 0 → 12 12 → 13 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 ===for (int row = 0; row < rows; row++)
pass 1 of 321System.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 pass row1 0 2 1 3 2 for (int col = 0; col < cols; col++)
pass 1 of 1223System.out.print("Row " + (row + 1) + ": ");24for (int col0 = 0; col < cols4; col++) {25 System.out.print(seating[row][col]1 + "\t");26}output1All 12 passes — pass 1 is the card above pass colseating[row][col]row1 0 1 0 2 1 2 0 3 2 3 0 4 3 4 0 5 0 5 1 6 1 6 1 7 2 7 1 8 3 8 1 9 0 9 2 10 1 10 2 11 2 11 2 12 3 12 2 System.out.println();
26 }27 System.out.println();28}System.out.println();
26 }27 System.out.println();28}System.out.println();
26 }27 System.out.println();28}totalSeats ← 12
30 int totalSeats→ 12 = rows3 * cols4;31 System.out.println("Total seats: " + totalSeats12);32}outputTotal seats: 12
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++) {for (int row = 0; row < rows; row++)
pass 1 of 511int 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 pass row1 0 2 1 3 2 4 3 5 4 seating[row][col] ← 1, seatNumber ← 2
pass 1 of 2012for (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 pass colrowseating[row][col]seatNumber1 0 0 0 → 1 1 → 2 2 1 0 0 → 2 2 → 3 3 2 0 0 → 3 3 → 4 4 3 0 0 → 4 4 → 5 5 0 1 0 → 5 5 → 6 6 1 1 0 → 6 6 → 7 7 2 1 0 → 7 7 → 8 8 3 1 0 → 8 8 → 9 9 0 2 0 → 9 9 → 10 ⋯ 9 more passes ⋯ 19 2 4 0 → 19 19 → 20 20 3 4 0 → 20 20 → 21 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 ===for (int row = 0; row < rows; row++)
pass 1 of 520System.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 pass row1 0 2 1 3 2 4 3 5 4 for (int col = 0; col < cols; col++)
pass 1 of 2022System.out.print("Row " + (row + 1) + ": ");23for (int col0 = 0; col < cols4; col++) {24 System.out.print(seating[row][col]1 + "\t");25}output120 passes — pass 1 is the card above pass colseating[row][col]row1 0 1 0 2 1 2 0 3 2 3 0 4 3 4 0 5 0 5 1 6 1 6 1 7 2 7 1 8 3 8 1 9 0 9 2 ⋯ 9 more passes ⋯ 19 2 19 4 20 3 20 4 System.out.println();
25 }26 System.out.println();27}System.out.println();
25 }26 System.out.println();27}System.out.println();
25 }26 System.out.println();27}System.out.println();
25 }26 System.out.println();27}System.out.println();
25 }26 System.out.println();27}totalSeats ← 20
29 int totalSeats→ 20 = rows5 * cols4;30 System.out.println("Total seats: " + totalSeats20);31}outputTotal seats: 20
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++) {for (int row = 0; row < rows; row++)
pass 1 of 311int 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 pass row1 0 2 1 3 2 seating[row][col] ← 1, seatNumber ← 2
pass 1 of 1812for (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 pass colrowseating[row][col]seatNumber1 0 0 0 → 1 1 → 2 2 1 0 0 → 2 2 → 3 3 2 0 0 → 3 3 → 4 4 3 0 0 → 4 4 → 5 5 4 0 0 → 5 5 → 6 6 5 0 0 → 6 6 → 7 7 0 1 0 → 7 7 → 8 8 1 1 0 → 8 8 → 9 9 2 1 0 → 9 9 → 10 ⋯ 7 more passes ⋯ 17 4 2 0 → 17 17 → 18 18 5 2 0 → 18 18 → 19 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 ===for (int row = 0; row < rows; row++)
pass 1 of 320System.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 pass row1 0 2 1 3 2 for (int col = 0; col < cols; col++)
pass 1 of 1822System.out.print("Row " + (row + 1) + ": ");23for (int col0 = 0; col < cols6; col++) {24 System.out.print(seating[row][col]1 + "\t");25}output118 passes — pass 1 is the card above pass colseating[row][col]row1 0 1 0 2 1 2 0 3 2 3 0 4 3 4 0 5 4 5 0 6 5 6 0 7 0 7 1 8 1 8 1 9 2 9 1 ⋯ 7 more passes ⋯ 17 4 17 2 18 5 18 2 System.out.println();
25 }26 System.out.println();27}System.out.println();
25 }26 System.out.println();27}System.out.println();
25 }26 System.out.println();27}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].
Access a specific seat
Look up a value using row and column indices.
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();
}
}
}
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 ===for (String day : days)
pass 1 of 529System.out.print(" ");30for (String dayMonday : days) {31 System.out.print(day.substring(0, 3) + "\t");32}outputMonAll 5 passes — pass 1 is the card above pass day1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday System.out.println();
32}33System.out.println();for (int r = 0; r < prices.length; r++)
pass 1 of 335for (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++) {outputFrontAll 3 passes — pass 1 is the card above pass rsections[r]1 0 Front 2 1 Middle 3 2 Back for (int c = 0; c < prices[r].length; c++)
pass 1 of 1536System.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$5015 passes — pass 1 is the card above pass crprices[r][c]1 0 0 50 2 1 0 50 3 2 0 55 4 3 0 55 5 4 0 75 6 0 1 40 7 1 1 40 8 2 1 45 9 3 1 45 ⋯ 4 more passes ⋯ 14 3 2 30 15 4 2 45 System.out.println();
39 }40 System.out.println();41}System.out.println();
39 }40 System.out.println();41}System.out.println();
39 }40 System.out.println();41}
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 ===for (String day : days)
pass 1 of 528System.out.print(" ");29for (String dayMonday : days) {30 System.out.print(day.substring(0, 3) + "\t");31}outputMonAll 5 passes — pass 1 is the card above pass day1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday System.out.println();
31}32System.out.println();for (int r = 0; r < prices.length; r++)
pass 1 of 334for (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++) {outputFrontAll 3 passes — pass 1 is the card above pass rsections[r]1 0 Front 2 1 Middle 3 2 Back for (int c = 0; c < prices[r].length; c++)
pass 1 of 1535System.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$5015 passes — pass 1 is the card above pass crprices[r][c]1 0 0 50 2 1 0 50 3 2 0 55 4 3 0 55 5 4 0 75 6 0 1 40 7 1 1 40 8 2 1 45 9 3 1 45 ⋯ 4 more passes ⋯ 14 3 2 30 15 4 2 45 System.out.println();
38 }39 System.out.println();40}System.out.println();
38 }39 System.out.println();40}System.out.println();
38 }39 System.out.println();40}
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 ===for (String day : days)
pass 1 of 528System.out.print(" ");29for (String dayMonday : days) {30 System.out.print(day.substring(0, 3) + "\t");31}outputMonAll 5 passes — pass 1 is the card above pass day1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday System.out.println();
31}32System.out.println();for (int r = 0; r < prices.length; r++)
pass 1 of 334for (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++) {outputFrontAll 3 passes — pass 1 is the card above pass rsections[r]1 0 Front 2 1 Middle 3 2 Back for (int c = 0; c < prices[r].length; c++)
pass 1 of 1535System.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$5015 passes — pass 1 is the card above pass crprices[r][c]1 0 0 50 2 1 0 50 3 2 0 55 4 3 0 55 5 4 0 75 6 0 1 40 7 1 1 40 8 2 1 45 9 3 1 45 ⋯ 4 more passes ⋯ 14 3 2 30 15 4 2 45 System.out.println();
38 }39 System.out.println();40}System.out.println();
38 }39 System.out.println();40}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.
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();
}
}
}
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 ===static void printSeating(int[][] seats)
pass 1 of 342static 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 4for (int r = 0; r < seats.length; r++)
pass 1 of 943System.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++) {output1All 9 passes — pass 1 is the card above pass r1 0 2 1 3 2 4 0 5 1 6 2 7 0 8 1 9 2 for (int c = 0; c < seats[r].length; c++)
pass 1 of 3645System.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}outputO36 passes — pass 1 is the card above pass crseats[r][c]1 0 0 0 2 1 0 0 3 2 0 0 4 3 0 0 5 0 1 0 6 1 1 0 7 2 1 0 8 3 1 0 9 0 2 0 ⋯ 25 more passes ⋯ 35 2 2 0 36 3 2 1 System.out.println();
48 }49 System.out.println();50}System.out.println();
48 }49 System.out.println();50}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 ===System.out.println();
48 }49 System.out.println();50}System.out.println();
48 }49 System.out.println();50}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 ===System.out.println();
48 }49 System.out.println();50}System.out.println();
48 }49 System.out.println();50}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 }for (int[] row : seats)
pass 1 of 333int available = 0;34for (int[] row : seats) {35 for (int seat : row) {for (int seat : row)
pass 1 of 1234for (int[] row : seats) {35 for (int seat1 : row) {36 if (seat == 0) available++;All 12 passes — pass 1 is the card above pass seat1 1 2 0 3 0 4 0 5 0 6 0 7 1 8 0 9 0 10 0 11 0 12 1 available ← 1
pass 1 of 935for (int seat : row) {36 if (seat0 == 0) available→ 1++;37}All 9 passes — pass 1 is the card above pass available1 0 → 1 2 1 → 2 3 2 → 3 4 3 → 4 5 4 → 5 6 5 → 6 7 6 → 7 8 7 → 8 9 8 → 9 System.out.println(" Available seats: " + available);
38 }39 System.out.println("\nAvailable seats: " + available9);40}output Available seats: 9
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 ===static void printSeating(int[][] seats)
pass 1 of 341static 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 4for (int r = 0; r < seats.length; r++)
pass 1 of 942System.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++) {output1All 9 passes — pass 1 is the card above pass r1 0 2 1 3 2 4 0 5 1 6 2 7 0 8 1 9 2 for (int c = 0; c < seats[r].length; c++)
pass 1 of 3644System.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}outputO36 passes — pass 1 is the card above pass crseats[r][c]1 0 0 0 2 1 0 0 3 2 0 0 4 3 0 0 5 0 1 0 6 1 1 0 7 2 1 0 8 3 1 0 9 0 2 0 ⋯ 25 more passes ⋯ 35 2 2 0 36 3 2 1 System.out.println();
47 }48 System.out.println();49}System.out.println();
47 }48 System.out.println();49}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 ===System.out.println();
47 }48 System.out.println();49}System.out.println();
47 }48 System.out.println();49}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 ===System.out.println();
47 }48 System.out.println();49}System.out.println();
47 }48 System.out.println();49}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 }for (int[] row : seats)
pass 1 of 332int available = 0;33for (int[] row : seats) {34 for (int seat : row) {for (int seat : row)
pass 1 of 1233for (int[] row : seats) {34 for (int seat1 : row) {35 if (seat == 0) available++;All 12 passes — pass 1 is the card above pass seat1 1 2 0 3 1 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 1 available ← 1
pass 1 of 934for (int seat : row) {35 if (seat0 == 0) available→ 1++;36}All 9 passes — pass 1 is the card above pass available1 0 → 1 2 1 → 2 3 2 → 3 4 3 → 4 5 4 → 5 6 5 → 6 7 6 → 7 8 7 → 8 9 8 → 9 System.out.println(" Available seats: " + available);
37 }38 System.out.println("\nAvailable seats: " + available9);39}output Available seats: 9
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 ===static void printSeating(int[][] seats)
pass 1 of 341static 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 4for (int r = 0; r < seats.length; r++)
pass 1 of 942System.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++) {output1All 9 passes — pass 1 is the card above pass r1 0 2 1 3 2 4 0 5 1 6 2 7 0 8 1 9 2 for (int c = 0; c < seats[r].length; c++)
pass 1 of 3644System.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}outputO36 passes — pass 1 is the card above pass crseats[r][c]1 0 0 0 2 1 0 0 3 2 0 0 4 3 0 0 5 0 1 0 6 1 1 0 7 2 1 0 8 3 1 0 9 0 2 0 ⋯ 25 more passes ⋯ 35 2 2 0 36 3 2 1 System.out.println();
47 }48 System.out.println();49}System.out.println();
47 }48 System.out.println();49}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 ===System.out.println();
47 }48 System.out.println();49}System.out.println();
47 }48 System.out.println();49}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 ===System.out.println();
47 }48 System.out.println();49}System.out.println();
47 }48 System.out.println();49}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 }for (int[] row : seats)
pass 1 of 332int available = 0;33for (int[] row : seats) {34 for (int seat : row) {for (int seat : row)
pass 1 of 1233for (int[] row : seats) {34 for (int seat1 : row) {35 if (seat == 0) available++;All 12 passes — pass 1 is the card above pass seat1 1 2 0 3 0 4 0 5 0 6 1 7 0 8 0 9 0 10 0 11 0 12 1 available ← 1
pass 1 of 934for (int seat : row) {35 if (seat0 == 0) available→ 1++;36}All 9 passes — pass 1 is the card above pass available1 0 → 1 2 1 → 2 3 2 → 3 4 3 → 4 5 4 → 5 6 5 → 6 7 6 → 7 8 7 → 8 9 8 → 9 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.
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);
}
}
}
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++) {for (int row = 0; row < data.length; row++)
pass 1 of 324int 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 pass row1 0 2 1 3 2 grandTotal ← 120
pass 1 of 925for (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 pass colrowdata[row][col]grandTotal1 0 0 120 0 → 120 2 1 0 135 120 → 255 3 2 0 150 255 → 405 4 0 1 80 405 → 485 5 1 1 95 485 → 580 6 2 1 110 580 → 690 7 0 2 200 690 → 890 8 1 2 180 890 → 1070 9 2 2 220 1070 → 1290 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 ===rowTotal ← 0
pass 1 of 336String[] 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 pass rowrowTotal1 0 0 2 1 0 3 2 0 rowTotal ← 120
pass 1 of 938int 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 pass colrowdata[row][col]rowTotal1 0 0 120 0 → 120 2 1 0 135 120 → 255 3 2 0 150 255 → 405 4 0 1 80 0 → 80 5 1 1 95 80 → 175 6 2 1 110 175 → 285 7 0 2 200 0 → 200 8 1 2 180 200 → 380 9 2 2 220 380 → 600 System.out.println(products[row] + ": " + rowTotal);
41 }42 System.out.println(products[row]Widgets + ": " + rowTotal405);43}outputWidgets: 405values this step0rowSystem.out.println(products[row] + ": " + rowTotal);
41 }42 System.out.println(products[row]Gadgets + ": " + rowTotal285);43}outputGadgets: 285values this step1rowSystem.out.println(products[row] + ": " + rowTotal);
41 }42 System.out.println(products[row]Gizmos + ": " + rowTotal600);43}outputGizmos: 600values this step2rowSystem.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 ===colTotal ← 0
pass 1 of 346System.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 pass colcolTotal1 0 0 2 1 0 3 2 0 colTotal ← 120
pass 1 of 948int 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 pass rowdata[row][col]colcolTotal1 0 120 0 0 → 120 2 1 80 0 120 → 200 3 2 200 0 200 → 400 4 0 135 1 0 → 135 5 1 95 1 135 → 230 6 2 180 1 230 → 410 7 0 150 2 0 → 150 8 1 110 2 150 → 260 9 2 220 2 260 → 480 System.out.println("Period " + (col + 1) + ": " + colTotal);
51 }52 System.out.println("Period " + (col0 + 1) + ": " + colTotal400);53}outputPeriod 1: 400System.out.println("Period " + (col + 1) + ": " + colTotal);
51 }52 System.out.println("Period " + (col1 + 1) + ": " + colTotal410);53}outputPeriod 2: 410System.out.println("Period " + (col + 1) + ": " + colTotal);
51 }52 System.out.println("Period " + (col2 + 1) + ": " + colTotal480);53}outputPeriod 3: 480
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++) {for (int row = 0; row < data.length; row++)
pass 1 of 423int 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 pass row1 0 2 1 3 2 4 3 grandTotal ← 1200
pass 1 of 1624for (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 pass colrowdata[row][col]grandTotal1 0 0 1200 0 → 1200 2 1 0 1350 1200 → 2550 3 2 0 1500 2550 → 4050 4 3 0 1400 4050 → 5450 5 0 1 800 5450 → 6250 6 1 1 950 6250 → 7200 7 2 1 1100 7200 → 8300 8 3 1 1050 8300 → 9350 9 0 2 2000 9350 → 11350 ⋯ 5 more passes ⋯ 15 2 3 700 18550 → 19250 16 3 3 650 19250 → 19900 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 ===rowTotal ← 0
pass 1 of 435String[] 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 pass rowrowTotal1 0 0 2 1 0 3 2 0 4 3 0 rowTotal ← 1200
pass 1 of 1637int 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 pass colrowdata[row][col]rowTotal1 0 0 1200 0 → 1200 2 1 0 1350 1200 → 2550 3 2 0 1500 2550 → 4050 4 3 0 1400 4050 → 5450 5 0 1 800 0 → 800 6 1 1 950 800 → 1750 7 2 1 1100 1750 → 2850 8 3 1 1050 2850 → 3900 9 0 2 2000 0 → 2000 ⋯ 5 more passes ⋯ 15 2 3 700 1100 → 1800 16 3 3 650 1800 → 2450 System.out.println(products[row] + ": " + rowTotal);
40 }41 System.out.println(products[row]Widgets + ": " + rowTotal5450);42}outputWidgets: 5450values this step0rowSystem.out.println(products[row] + ": " + rowTotal);
40 }41 System.out.println(products[row]Gadgets + ": " + rowTotal3900);42}outputGadgets: 3900values this step1rowSystem.out.println(products[row] + ": " + rowTotal);
40 }41 System.out.println(products[row]Gizmos + ": " + rowTotal8100);42}outputGizmos: 8100values this step2rowSystem.out.println(products[row] + ": " + rowTotal);
40 }41 System.out.println(products[row]Sprockets + ": " + rowTotal2450);42}outputSprockets: 2450values this step3rowSystem.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 ===colTotal ← 0
pass 1 of 445System.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 pass colcolTotal1 0 0 2 1 0 3 2 0 4 3 0 colTotal ← 1200
pass 1 of 1647int 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 pass rowdata[row][col]colcolTotal1 0 1200 0 0 → 1200 2 1 800 0 1200 → 2000 3 2 2000 0 2000 → 4000 4 3 500 0 4000 → 4500 5 0 1350 1 0 → 1350 6 1 950 1 1350 → 2300 7 2 1800 1 2300 → 4100 8 3 600 1 4100 → 4700 9 0 1500 2 0 → 1500 ⋯ 5 more passes ⋯ 15 2 2100 3 2450 → 4550 16 3 650 3 4550 → 5200 System.out.println("Period " + (col + 1) + ": " + colTotal);
50 }51 System.out.println("Period " + (col0 + 1) + ": " + colTotal4500);52}outputPeriod 1: 4500System.out.println("Period " + (col + 1) + ": " + colTotal);
50 }51 System.out.println("Period " + (col1 + 1) + ": " + colTotal4700);52}outputPeriod 2: 4700System.out.println("Period " + (col + 1) + ": " + colTotal);
50 }51 System.out.println("Period " + (col2 + 1) + ": " + colTotal5500);52}outputPeriod 3: 5500System.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.
Find a value's position
Search for a value and return its row/column.
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();
}
}
}
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;for (int row = 0; row < seatMap.length; row++)
pass 1 of 319for (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 pass rowseatMap[row][col]coltargetSeatfoundRowfoundCol1 0 — — — — — 2 1 — — — — — 3 2 C3 2 C3 2 2 for (int col = 0; col < seatMap[row].length; col++)
pass 1 of 1319for (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 pass colrowseatMap[row][col]targetSeatfoundRowfoundCol1 0 0 — — — — 2 1 0 — — — — 3 2 0 — — — — 4 3 0 — — — — 5 4 0 — — — — 6 0 1 — — — — 7 1 1 — — — — 8 2 1 — — — — 9 3 1 — — — — ⋯ 2 more passes ⋯ 12 1 2 — — — — 13 2 2 C3 C3 2 2 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 }if (foundRow != -1)
26 }27 if (foundRow2 != -1) break; // Exit outer loop too28}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: C3if (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.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 ===for (int row = 0; row < seatMap.length; row++)
pass 1 of 446System.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 pass rowfoundRowcolfoundColseatMap[row][col]1 0 — — — — 2 1 — — — — 3 2 2 2 2 C3 4 3 — — — — for (int col = 0; col < seatMap[row].length; col++)
pass 1 of 2047for (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 pass colrowfoundRowfoundColseatMap[row][col]1 0 0 — — — 2 1 0 — — — 3 2 0 — — — 4 3 0 — — — 5 4 0 — — — 6 0 1 — — — 7 1 1 — — — 8 2 1 — — — 9 3 1 — — — ⋯ 9 more passes ⋯ 19 3 3 — — — 20 4 3 — — — else
pass 1 of 1950 System.out.print("[" + seatMap[row][col] + "]\t");51} else {52 System.out.print(" " + seatMap[row][col]A1 + " \t");53}output A119 passes — pass 1 is the card above pass seatMap[row][col]rowcolfoundRowfoundCol1 A1 0 0 — — 2 A2 0 1 — — 3 A3 0 2 — — 4 A4 0 3 — — 5 A5 0 4 — — 6 B1 1 0 — — 7 B2 1 1 — — 8 B3 1 2 — — 9 B4 1 3 — — ⋯ 8 more passes ⋯ 18 D4 3 3 — — 19 D5 3 4 — — System.out.println();
54 }55 System.out.println();56}System.out.println();
54 }55 System.out.println();56}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]System.out.println();
54 }55 System.out.println();56}System.out.println();
54 }55 System.out.println();56}
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;for (int row = 0; row < seatMap.length; row++)
pass 1 of 418for (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 pass row1 0 2 1 3 2 4 3 for (int col = 0; col < seatMap[row].length; col++)
pass 1 of 2018for (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 pass colrow1 0 0 2 1 0 3 2 0 4 3 0 5 4 0 6 0 1 7 1 1 8 2 1 9 3 1 ⋯ 9 more passes ⋯ 19 3 3 20 4 3 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: Z9else
39 ", then count " + (foundCol + 1) + " seat(s) from the left.");40} else {41 System.out.println("Seat not found!");42}outputSeat not found!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 ===for (int row = 0; row < seatMap.length; row++)
pass 1 of 445System.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 pass row1 0 2 1 3 2 4 3 for (int col = 0; col < seatMap[row].length; col++)
pass 1 of 2046for (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 pass colrow1 0 0 2 1 0 3 2 0 4 3 0 5 4 0 6 0 1 7 1 1 8 2 1 9 3 1 ⋯ 9 more passes ⋯ 19 3 3 20 4 3 else
pass 1 of 2049 System.out.print("[" + seatMap[row][col] + "]\t");50} else {51 System.out.print(" " + seatMap[row][col]A1 + " \t");52}output A120 passes — pass 1 is the card above pass seatMap[row][col]rowcol1 A1 0 0 2 A2 0 1 3 A3 0 2 4 A4 0 3 5 A5 0 4 6 B1 1 0 7 B2 1 1 8 B3 1 2 9 B4 1 3 ⋯ 9 more passes ⋯ 19 D4 3 3 20 D5 3 4 System.out.println();
53 }54 System.out.println();55}System.out.println();
53 }54 System.out.println();55}System.out.println();
53 }54 System.out.println();55}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