Accessing 2D Array Elements
Part of the Logic & Flow section of Coddy's Java journey — lesson 2 of 59.
Accessing elements in a 2D array is similar to accessing elements in a 1D array, but instead of using a single index, you use two indices: one for the row and one for the column. Remember that indices in Java start at 0, so the first row is at index 0, the second row is at index 1, and so on. Similarly, the first column is at index 0, the second column is at index 1, and so forth.
To access an element, you first specify the row index and then the column index, using the following syntax:
arrayName[rowIndex][columnIndex]For example, to access the element in the second row (index 1) and third column (index 2) of a 2D array named matrix, you would write:
int element = matrix[1][2];This would assign the value of the element at row 1, column 2 to the variable element.
Getting 2D array dimensions:
Just like 1D arrays, you can use .length to get dimensions of a 2D array:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
int numRows = matrix.length; // Gets number of rows: 2
int numCols = matrix[0].length; // Gets number of columns: 3matrix.lengthreturns the number of rowsmatrix[0].lengthreturns the number of columns in the first row
Checking bounds:
Before accessing an element, it's good practice to check if the indices are valid:
if (rowIndex >= 0 && rowIndex < matrix.length &&
colIndex >= 0 && colIndex < matrix[0].length) {
// Safe to access matrix[rowIndex][colIndex]
}Challenge
EasyCreate a method named getElement that takes three arguments:
- A 2D array of integers named
matrix. - An integer
rowIndexrepresenting the row index. - An integer
colIndexrepresenting the column index.
The method should return the element at the specified row and column in the matrix. If the rowIndex or colIndex is out of bounds (i.e., less than 0 or greater than or equal to the number of rows or columns), the method should return -1.
Cheat sheet
To access elements in a 2D array, use two indices: one for the row and one for the column. Indices start at 0.
Syntax:
arrayName[rowIndex][columnIndex]Example:
int element = matrix[1][2]; // Gets element at row 1, column 2you can use .length to get dimensions of a 2D array:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
int numRows = matrix.length; // Gets number of rows: 2
int numCols = matrix[0].length; // Gets number of columns: 3Try it yourself
// Write your code only inside the class. Do not write main() or any code outside this class.
class GetElement {
public static int getElement(int[][] matrix, int rowIndex, int colIndex) {
// Write your code here
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays2HashMap Part 1
What is a HashMap?Declare a HashMapAccessing ValuesCheck If Key ExistsModifying DictionariesRecap - HashMap