Skip to content

Accessing

To access the elements of a two-dimensional array, we first tell Java which array we mean.

int[][] arr2d = new int[5][5];

int[] arrayAtIndex0 = arr2d[0];

Accessing arrays

After telling Java which array, we can use another [] to say which element.

int x = arrayAtIndex0[3];

Accessing arrays

We are telling Java which row and column we want.

  • We can access elements in one statement.
int[][] arr2d = new int[5][5];
int x2 = arr2d[0][3];

Accessing arrays

Practice Exercise

Think of 2-d arrays in terms of "row" and "column", not x and y coordinates.

Assigning Elements

Assigning elements is simple - just tell Java which row and column you want.

int[][] arr2d = new int[5][5];
arr2d[3][2] = 42;
Accessing arrays

Drill

AdvancedArrays/com.example.advancedarrays.drills.MultiDeclare * Fill the multi-dimensional array as directed.


Prev -- Up -- Next