8.1: Declaring + Initializing 2D Arrays; Determining their
Review:
Arrays are a collection (list) of elements (primitive or object reference type data)
So, a 2-Dimensional array is an array where the elements within that array are other arrays
2D arrays can be better at storing certain types of information
- Especially if the data represents a space, with rows and columns
Seating chart:
| . | Column 1 | Column 2 | Column 3 |
| - | - | - | - |
| Row 1 | Abby | Ben | Clara | |
| Row 2 | Ethan | Frank | | |
| Row 3 | Isabelle | John | Kim | Leo |
Note that this is a non-rectangular 2D array
- Or if the data needs to be classified under categories
. |
Month 1 |
Month 2 |
Month 3 |
Winter |
December |
January |
February |
Spring |
March |
April |
May |
Summer |
June |
July |
August |
Fall |
Summer |
October |
November |
This is a rectangular 2D array. Non-rectangular 2D arrays are not a part of the CSA course
Declaring a 2D array </h2>
2D Arrays can be declared like this:
``` dataType[][] nameOfArray; ```
Initializing a 2D array
``` new dataType[r][c]; ```
r: number of rows (number of arrays)
c: number of columns (length of each array)
```java
public class Seasons {
private String[][] Seasons = new String[2][3];
// Or, if you already know what the elements should be:
private String[][] Seasons2 = {
{"December", "January", "February"},
{"March", "April", "May"},
{"June", "July", "August"},
{"September", "October", "November"}
};
}
```
### Size of 2D Arrays
The size of the 2D array is classified by number of rows by number of columns
Number of rows can be found like this:
``` r = trimesterCourses.length ```
This would give the number of arrays within the 2D array, since each array is an element
For number of columns:
``` c = trimesterCourses[0].length ```
This finds the number of elements of the first array within the 2D array.
Accessing the Elements of a 2D Array
The elements of a 2D array can be accessed using index
``` Seasons[0][2] ```
**Output: February**
the value in the first bracket is the index of the rows, or which array we are accessing. In this case, the 0th index means we are accessing the first array
The value in the second bracket is the index within the array. So we are looking for the 2nd value within the first array.
To update the element of a 2D array, all you need to do is reference its location and change the value.
```java
public class Seasons {
private String[][] seasons = new String[2][3];
private static String[][] seasons2 = {
{"December", "January", "February"},
{"March", "April", "May"},
{"June", "July", "August"},
{"September", "October", "November"}
};
public static void main(String[] args) {
System.out.println(seasons2[0][2]);
seasons2[0][2] = "Changed Value";
System.out.println(seasons2[0][2]);
}
}
Seasons.main(null);
```
### Popcorn Hack:
```java
public class TrimesterGrades {
private int[][] trimesterGrades = {
{85, 90, 78, 92, 99}, // tri 1
{92, 88, 91, 97, 80}, // tri 2
{79, 85, 83, 95, 67} // tri 3
};
}
```
The 2D array keeps track of a students grade, grouped by each trimester.
The student, currently in Trimester 3, retook a test in their 3rd period, which raised that grade to 90.
Show how they would write code that changes the grade for the 3rd period class
```java
public class TrimesterGrades {
private static int[][] trimesterGrades = {
{85, 90, 78, 92, 99}, // tri 1
{92, 88, 91, 97, 80}, // tri 2
{79, 85, 83, 95, 67} // tri 3
};
public static void main(String[] args) {
System.out.println(trimesterGrades[2][4]); // printing 67
trimesterGrades[2][4] = 90; // assigning 90 to where 67 is
System.out.println(trimesterGrades[2][4]); // printing the updated value
}
}
TrimesterGrades.main(null)
```
67
90
8.2 - Traversing 2D Arrays
- Learning Objective: Using nested + enhanced for loops to traverse arrays
- How to use a nested loop to traverse a 2d array
- Have an outer loop to iterate through each row
- Have an inner loop that iterates through each column
- It traverses from left to right and goes up to down (ABCDE → FGHIJ)
![image](https://github.com/John-sCC/jcc_frontend/assets/82348259/b93a73c1-8b76-4acb-a2f5-f51dc704f21c)
- How to use enhanced for loops
- Get each row within the grid
- Get each value within each row
![image](https://github.com/John-sCC/jcc_frontend/assets/82348259/5f02267f-8a55-4248-8a9a-ca9be24fa2cd)
- How to search for values in a 2d array
- Use nested loops and in the inner loop make an if statement that returns True if a certain value is found
- Finding the max/min
- Save the index of the extrema and in the inner loop have a comparison statement that determines that value