Unit 8 Notes
Initializing and setting values to an Arraylist
- The first step is to initialize the array. To do this, specify the type, followed by “[][]”, followed by the name of the 2D array. Examples:
int[][] numbers; String[][] names; char[][] letters; float[][] floats; double[][] doubles; Object[][] objects;
Then you must give it a value, by setting the 2D array. There are two ways to set a 2D array. Method 1: You can set the array by giving it values Method 2: You can also initialize it by giving it the size
Loops with 2D array
To iterate through a normal array, you would could use a for loop, as demonstrated below to print the alphabet. You would iterate through the first array(row), then use another for loop nested within the first that iterates through the second arrays(columns).
arr.length() represents the length of the row.