2D Arrays

What are 2D Arrays?

2D Arrays can be thought of as matrices. They are arrays of arrays that allow you to store information in a table with rows and columns.

The outer array can be thought of as the rows and the inner arrays the columns. 

Visualization of a 2D Array.

To declare:

int[][] name = new int[3][5];  // 3 is rows, 5 is columns

To declare and initialize:

int[][] name = {{1,2,3}, {3,2,4},{1,2,3}};

To access and set:

name[1][1] = 5; 
name.length //length of the columns
name[0].length //length of the rows

When arrays are created their contents are automatically initialized to 0 for numeric types, null for object references, and false for type boolean. 

What is iteration?

For iterating over the 2D array, use two for loops to go through the rows and each column. 

for(int i = 0; i ⋖ array.length; i++){
	for(int j = 0; j ⋖ array[0].length; i++){
		array[i][j] = 1;
	}
}
Full course
Next Lesson