Alright, it’s time to tackle 2D arrays and arrayLists. 2D arrays are basically arrays of arrays. First off, 2D arrays:

### 2D Arrays

To start off with, the declaration and instantiation. Since 2D arrays are objects, we will use the *new* operator. There are several ways to do this:

The “normal” way:

data type[] []name= newdata type[# of rows] [# of collumns];Example: I want to create a 2D array called nums containing int data with 2 rows and 3 collumns:

int[] [] nums = new int[2][3];

If you want to create a jagged array where each row is of a different length, then you can input your values directly into the 2D array. There are several ways to do this but I’m going to show you guys the simplest one:

int[] [] nums =

{

{4, 5, 6, 4},

{7, 234, 34, 1},

{23, 57, 2}

};

(Of course, you can write this all on one line but this layout looks more intuitive.)

So, this is what the 2D array would look like visually:

If this were a rectangular 2D array and not a jagged array, any attempt to retrieve a data value from the index position (3, 2) would return **null **so keep that in mind if you are manipulating or accessing a 2D array. There is also a thing called sparseArrays and an FRQ concerning sparseArrays has popped up on the AP exams (although I don’t think it’s likely to pop up again but hey, the more you know…) so here’s a link that kind of explains it but I don’t really think you need to know it for this class because it’s a little more high-level.

### Traversing Through a 2D Array

With 2D arrays, you also need to know how to traverse through them. You can do this through the basic three types of loops. I’m going to show an example of each.

#### For Loops

You can use both the for loop and a for each loop. In my examples, I’m going to use the loops to add up all the numbers in the 2D array. I’m going to use the nums array that I instantiated above (all of the loops work with regular 2D arrays as well as jagged 2D arrays). This is how to use a regular nested for loop to go through every element in a 2D array and add them up.

int sum = 0;

for (int row = 0; row < nums.length; row++)

for (int col = 0; col < nums[row].length; col++)

sum += nums[row][col];

**For Each Loop**

For each loops are a little less intuitive to use but in the first part of the parameters denote which type of data type you’re trying to handle within the array and the second part is the array that you want to traverse through.

int sum = 0;

for (int[] num : nums) //this looks at each row of thes 2D array

for (int i : num) //this looks at all the data values within each row

sum+= num;

Both of these loops do the same thing.

### While Loops

As a rule of thumb, while loops are used when you don’t know how many times iterations you need the loop to go through. I’m just going to use the basic example I used above and write a while loop instead.

int row = 0, col = 0, sum = 0;

while (row < nums.length) {

while (col < nums[row].length) {

sum += nums[row][col];

col++; }

row++; }

## Leave a Reply