2d Arrys
2d Arrys
Adapted from:
1) Building Java Programs: A Back to Basics Approach
by Stuart Reges and Marty Stepp
2) Runestone CSAwesome Curriculum
https://longbaonguyen.github.io
2D Arrays
2
2D Arrays
A two-dimensional (2D) array has rows and columns.
3
2D Arrays
Two dimensional arrays are especially useful when the data is naturally
organized in rows and columns like in a spreadsheet, bingo, battleship,
theater seats, classroom seats, connect-four game, or a picture.
4
2D Arrays
Many programming languages actually store two-dimensional array data in a
one-dimensional array. The typical way to do this is to store all the data for
the first row followed by all the data for the second row and so on. This is
called row-major order.
Some languages store all the data for the first column followed by all the data
for the second column and so on. This called column-major order.
5
Declare and Initialize
To declare and initialize a 2D array,
where row, col is the number of rows/columns. When arrays are created their
contents are automatically initialized to 0 for numeric types, null for object
references, and false for type boolean.
0 0 0 0
0 0 0 0
0 0 0 0
6
2D Array
To explicitly put a value in an array, you can use assignment
statements specifying the row and column of the entry.
2 0 0 0
0 0 -6 0
0 7 0 0
7
Initializer List
You can also initialize (set) the values for the array when you
create it. In this case you don’t need to specify the size of the
array, it will be determined from the values you give. This is called
using initializer list.
3 4 5
6 7 8
8
Declare and Initialize
Declaring and initializing 2D arrays.
int[][] table; //2D array of ints, null reference
9
Array of Arrays
A 2D array is implemented as an array of row arrays. Each row is a one-
dimensional array of elements. Suppose that mat is the 2D array:
3 -4 1 2
6 0 8 1
-2 9 1 7
10
Array of Arrays
3 -4 1 2
6 0 8 1
-2 9 1 7
3) Java allows “jagged arrays” where each row array may have different
lengths. However, on the AP exam, assume all arrays are
rectangular.
11
Example
int[][] mat={{3,4,5},{1,2},{0,1,-3,5}};
mat[0] = {3,4,5}
mat[1] = {1,2}
mat[2] = {0,1,-3,5}
mat.length = 3
mat[0].length = 3
mat[1].length = 2
mat[2].length = 4
12
Common Algorithms
You should know how to implement the following algorithms:
Given a 2D array:
18
searching an array
Write the method that searches a 2D array for a target value. Returns
true if target is in the 2D array and false otherwise. Assume the
sequentialSearch for 1D below has already been implemented. (see
previous lecture).
public boolean sequentialSearch(int[] a, int target)
{…}
20
2D Arrays of Objects
The Student class is used in the following SeatingChart class.
21
Regular For Loop
public double sum(){
double sum = 0.0;
for(int i = 0; i < seats.length; i++){
for(int j = 0; j < seats[0].length; j++){
sum += seats[i][j].getGpa();
}
}
return sum;
}
We can do this more simply with a for each loop! See next slide.
22
For Each Loop
public double sum(){
double sum = 0.0;
for(Student[] row: seats){
for(Student std: row){
sum += std.getGpa();
}
}
return sum;
}
23
2D Arrays of Objects
The Student class is used in the following SeatingChart class.
14
25
36
28
Lab 2
A magic square is an N x N array of numbers such that
1. Every number from 1 through N2 must appear exactly once.
2. Every row, column, major and minor diagonal must add up to
the same total.
A template can be found on replit for this lab: (fork the repl)
https://repl.it/@LongNguyen18/MagicSquareLab
16 3 2 13
Example: N=4
5 10 11 8
9 6 7 12
4 15 14 1
29
Lab 2
Write the class MagicSquare with instance methods given in the
next few slides. MagicSquare should have an instance 2D array
variable square. MagicSquare should have a constructor that
accepts a 2D array.
You must use the method headers indicated for each method.
Write a driver class with a main method to test your MagicSquare
class.
30
Lab 2
31
Lab 2
Returns whether both the major and minor diagonal sums are
equal to sum. The major and minor diagonal are highlighted
below.
public boolean diagSums(int sum){…}
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
32
Lab 2
public boolean exactlyOnce(){…}
Returns true if the numbers 1 to N2 occurs exactly once in
square and false otherwise. N is the number of rows(and
columns) in square. Hint: Use a tally array discussed in the
array algorithms lecture.
You must use the each of the above methods to write the
following isMagic method.
public boolean isMagic(){…}
Returns true if square is magic and false otherwise.
A template can be found on replit for this lab here: (fork the repl)
https://repl.it/@LongNguyen18/MagicSquareLab
33
Lab 3(Grid)
34