2D Arrays in Java Tutorial

storing data

<aside> 🦕 arrays: multiple space to allocate; pointing towards a data structure;

</aside>

copying arrays

when we want to make a copy of an array, the copied array will be exactly like the original and change whenever we change an element value in the original value (will change the value for both of the values)


when we have each element in the array pointing to a new array for each, then we get a 2D array

<aside> 🚙 2D arrays: work as a grid as opposed to a single line of data storage

</aside>

declaring and initializing

int[][]num; //2D array of integers
int[][] num = new int[3][5]; //allocates space for a 3 by 5 array 
//3 rows, 5 columns
int[][]num = 
{{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}}; //2D array of 3 rows and 5 columns

accessing elements

num[1][3] //returns 9
num[row index][column index]