Two-dimensional arrays help you store data in a table-like form with rows and columns. This is useful when you want to organize information in grids, like a chessboard or a calendar.
Two-dimensional arrays in Java
public class TwoDimensionalArrayExample { public static void main(String[] args) { // Declare and create a 2D array with 3 rows and 4 columns int[][] matrix = new int[3][4]; // Assign a value to a specific cell matrix[0][0] = 5; // Access a value from the array int value = matrix[0][0]; } }
The first bracket [] represents rows, and the second [] represents columns.
Array indices start at 0, so the first row is index 0 and the first column is index 0.
int[][] emptyArray = new int[0][0];
int[][] singleElementArray = {{42}};
int[][] jaggedArray = new int[3][]; jaggedArray[0] = new int[2]; jaggedArray[1] = new int[4]; jaggedArray[2] = new int[3];
int[][] predefinedArray = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
This program creates a 2D array with 2 rows and 3 columns. It first prints the array with default values (which are zeros). Then it fills the array with multiplication results of (row + 1) and (col + 1) and prints the updated array.
public class TwoDimensionalArrayDemo { public static void main(String[] args) { // Create a 2D array with 2 rows and 3 columns int[][] table = new int[2][3]; // Print the array before assigning values System.out.println("Before assigning values:"); print2DArray(table); // Assign values to each cell for (int row = 0; row < table.length; row++) { for (int col = 0; col < table[row].length; col++) { table[row][col] = (row + 1) * (col + 1); } } // Print the array after assigning values System.out.println("After assigning values:"); print2DArray(table); } // Helper method to print a 2D array public static void print2DArray(int[][] array) { for (int row = 0; row < array.length; row++) { for (int col = 0; col < array[row].length; col++) { System.out.print(array[row][col] + " "); } System.out.println(); } } }
Accessing or assigning an element uses array[row][column] syntax.
Time complexity to access or assign one element is O(1).
Be careful not to access indexes outside the array size to avoid errors.
Two-dimensional arrays store data in rows and columns like a table.
Use array[row][column] to access or change values.
They are useful for grids, matrices, and tabular data.
Practice
Solution
Step 1: Understand the structure of two-dimensional arrays
Two-dimensional arrays store data in rows and columns, like a grid or table.Step 2: Compare options with the definition
Only A table with rows and columns correctly describes this as a table with rows and columns.Final Answer:
A table with rows and columns -> Option CQuick Check:
Two-dimensional array = table [OK]
- Confusing with one-dimensional arrays
- Thinking it's a list of unrelated variables
- Mixing arrays with methods
Solution
Step 1: Recall Java syntax for 2D array declaration
Java uses int[][] name = new int[rows][columns]; to declare 2D arrays.Step 2: Check each option
int[][] matrix = new int[3][4]; matches correct syntax. int matrix[3][4]; is C-style, invalid in Java. int matrix = new int[3,4]; uses comma, invalid. int[] matrix = new int[3][4]; declares a 1D array incorrectly.Final Answer:
int[][] matrix = new int[3][4]; -> Option BQuick Check:
Correct 2D array syntax = int[][] matrix = new int[3][4]; [OK]
- Using commas instead of separate brackets
- Trying C-style declarations
- Mixing 1D and 2D array syntax
int[][] arr = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(arr[1][0]);Solution
Step 1: Identify the element at arr[1][0]
arr[1] is the second row: {3, 4}. The element at index 0 in this row is 3.Step 2: Confirm the printed value
System.out.println prints arr[1][0], which is 3.Final Answer:
3 -> Option AQuick Check:
arr[1][0] = 3 [OK]
- Confusing row and column indices
- Starting count at 1 instead of 0
- Mixing up elements in nested arrays
int[][] grid = new int[2][3]; grid[2][1] = 5;
Solution
Step 1: Check array size and indices used
Array grid has 2 rows (indices 0 and 1) and 3 columns (indices 0 to 2). Accessing grid[2][1] uses row index 2 which is out of bounds.Step 2: Identify the error type
Accessing an invalid index causes ArrayIndexOutOfBoundsException at runtime.Final Answer:
Array index out of bounds error -> Option AQuick Check:
Index 2 invalid for 2-row array [OK]
- Using indices equal to array length
- Confusing rows and columns sizes
- Assuming arrays auto-expand
int[][] data = {{1,2},{3,4},{5,6}};, which code snippet correctly calculates the sum of all elements?Solution
Step 1: Understand array dimensions and iteration
data.length is number of rows (3). data[i].length is number of columns (2). Nested loops must iterate rows then columns.Step 2: Analyze each option's loop structure
int sum = 0; for(int i=0; iFinal Answer:
The nested loops with outer loop on data.length and inner on data[i].length -> Option DQuick Check:
Correct nested loops sum all elements [OK]
- Swapping row and column loops
- Adding arrays directly to int
- Using wrong indices in loops
Ready to prove it?
Pass the test and this pattern moves unknown
