Two Dimensional Arrays in Java
- Two-dimensional (2D) arrays in Java.
- Declaration, initialization, access, and traversal with for and for-each loops will be covered.
Declaring and Initializing 2D Arrays
- Declaring a 2D array variable:
java
int[][] a;
- Two sets of square brackets indicate a 2D array of integers.
- Initializing a 2D array:
java
a = new int[3][4];
- The first number (3) specifies the number of rows.
- The second number (4) specifies the number of columns.
- Elements are initialized to the default value (0 for int).
Visualizing 2D Arrays
- Traditional visualization: a grid with rows and columns.
- Alternative visualization: an array of arrays.
- The first dimension is an array of references to other arrays.
- Each element in the first array points to a one-dimensional array (a row).
- Example:
java
int[][] b = {{4, 2, 0}, {6, 3, 7}};
b is an array of size 2.b[0] is an array {4, 2, 0} of size 3.b[1] is an array {6, 3, 7} of size 3.
Accessing 2D Array Elements
- Accessing elements using row and column indices:
java
System.out.println(b[0][1]); // Outputs 2
a[1][2] = 9;
- Boolean expression evaluation example:
java
a[2][0] = 0;
b[1][1] = 3;
System.out.println(a[2][0] < b[1][1]); // Evaluates to true (0 < 3)
Traversing 2D Arrays Using For Loops
- Example array:
java
int[][] arr = {{2, 3, 1}, {8, 5, 6}};
- Traversing with nested for loops:
java
for (int r = 0; r < arr.length; r++) {
for (int c = 0; c < arr[r].length; c++) {
System.out.println(arr[r][c]);
}
System.out.println(); // New line after each row
}
- The outer loop iterates through the rows (outer array).
arr.length gives the number of rows.- The inner loop iterates through the columns (inner arrays).
arr[r].length gives the number of columns in row r.
- 2D arrays are row-major in Java (row index comes first).
- Modifying values during traversal:
java
for (int r = 0; r < arr.length; r++) {
for (int c = 0; c < arr[r].length; c++) {
arr[r][c] *= 2; // Double each element
}
}
- Modifying the increment (e.g.,
r += 2).
Traversing 2D Arrays Using For-Each Loops
- Example array:
java
int[][] nums = {{4, 2}, {6, 3}};
- Traversing with nested for-each loops:
java
for (int[] row : nums) {
for (int value : row) {
System.out.println(value);
}
System.out.println(); // New line after each row
}
- The outer loop iterates through the rows (1D arrays of int).
- The inner loop iterates through the values in each row (int values).
- Modifying the temporary variable
value does not change the original array.
FRQ: Nevada Smith and the Temple of Arrays
- Problem Description: An archaeologist is in a room with a floor represented by a 2D array of strings (symbols).
- Conditions for a safe-to-cross floor:
- At least one straight line of identical tiles from left to right.
- No danger symbols (
x or y). - Not all symbols on the floor are the same.
- Methods to implement:
lineOfIdentical(String[] arr): Returns true if all symbols in the array are the same, false otherwise.isFloorSafe(String[][] floor): Returns true if the floor is safe (no danger symbols and not all symbols are the same), false otherwise.canCrossFloor(String[][] floor): Returns true if there is a safe path across the floor (at least one row has identical symbols and the floor is safe), false otherwise.
Part a: Line of Identical Solution
public boolean lineOfIdentical(String[] arr) {
for (int i = 1; i < arr.length; i++) {
if (!arr[i].equals(arr[0])) {
return false;
}
}
return true;
}
- Compares each value in the array to the value at index 0 using the
.equals() method. - If any value is not equal to the first, returns
false immediately. - If the entire loop completes without finding a difference, returns
true.
Part b: Is Floor Safe Solution
public boolean isFloorSafe(String[][] floor) {
String firstSymbol = floor[0][0];
boolean allSymbolsSame = true;
for (String[] row : floor) {
for (String symbol : row) {
if (symbol.equals("x") || symbol.equals("y")) {
return false;
}
if (!symbol.equals(firstSymbol)) {
allSymbolsSame = false;
}
}
}
return !allSymbolsSame;
}
- Checks for danger symbols (
x or y) and ensures that not all symbols are the same. - Returns
false immediately if a danger symbol is found. - Sets
allSymbolsSame to false if any symbol differs from the first symbol. - After traversing the entire array, returns
true only if allSymbolsSame is false.
Part c: Can Cross Floor Solution
public boolean canCrossFloor(String[][] floor) {
if (!isFloorSafe(floor)) {
return false;
}
for (String[] row : floor) {
if (lineOfIdentical(row)) {
return true;
}
}
return false;
}
- First, checks if the floor is safe using the
isFloorSafe method. - If the floor is not safe, returns
false immediately. - Then, iterates through each row of the floor and calls the
lineOfIdentical method. - If any row has identical symbols, returns
true immediately. - If no row has identical symbols after traversing the entire array, returns
false.