Unit 8 - 2D Arrays
Notes, Hacks, and HW for 2D Arrays.
- Notes
- 2017 FRQ #4a
- 4b
Notes
- Array: Stores a collection of primitive data
- Element: A value in the array
- Index: The position where an element is, starting from 0
To create a 2D Array, type data type[][] name = new data type[num row][num column]
You can identify the length of an array with .length
.
The example below accesses the last element in an array using .length. This is especially useful for when you are not sure of the sizes of an array.
public class TwoDArray{
public static void main(String[] args) {
int[][] arr = {{1, 2, 3}, {4, 5, 6}};
System.out.println(arr[arr.length - 1][arr[arr.length-1].length - 1]);
}
}
TwoDArray.main(null)
To iterate through a 2D array, nested for loops are extremely useful. Below is an exmpale;
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "a", "f", "g", "l" },
{ "b", "e", "h", "k" },
{ "c", "d", "i", "j" }
};
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 4; col++) {
System.out.print(arr[row][col] + " ");
}
System.out.println(" ");
}
}
}
Test.main(null);
2D Array Vocab:
- Array = a data structure used to implement a collection (list) of primitive or object reference data
- Element = a single value in the array
- Index = the position of the element in the array (starts from 0)
-
Array Length = the number of elements in the array
- Is public, so can be accessed in any class
- Is also final, so can’t change it after array has been created
The Basics:
- A 2D array is an array of arrays, and can be a better way to store data
- Declaring a 2D array:
DataType[][] nameOf2DArray
- Initializing a 2D array
-
DataType[][] nameOf2DArray = new DataType[r][c];
- r = # of rows
- The # of arrays in the array
-
r = list.length
- c = # of columns
- The # of elements in the inner arrays
c = list[0].length
- r = # of rows
-
Initializing a Sample Array:
public class Test {
public static void main(String[] args) {
int[][] arr = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
System.out.println("arr[0][0] = " + arr[0][0]);
System.out.println("arr[1][2] = " + arr[1][2]);
System.out.println("arr[2][1] = " + arr[2][1]);
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "a", "f", "g" },
{ "b", "e", "h" },
{ "c", "d", "i" }
};
// Print the last element in the array!
System.out.println(arr[2][2]);
}
}
Test.main(null);
- a quick tip for the future:
list[list.length - 1][list[0].length - 1]
- Updating an element:
list[r][c] = value;
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
// Change Austin to Athens and print!
System.out.println("Change Austin to Athens and print!");
arr[2][1] = "Athens";
System.out.println(arr[2][1]);
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "a", "f", "g", "l" },
{ "b", "e", "h", "k" },
{ "c", "d", "i", "j" }
};
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 4; col++) {
System.out.print(arr[row][col] + " ");
}
System.out.println(" ");
}
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
// Print out the array without using numerical values!
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[row].length; col++) {
System.out.print(arr[row][col] + " ");
}
System.out.println();
}
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
String match = "";
String name = "Boston";
for (String[] row : arr) {
for (String item : row) {
if (item.equals(name)) {
match = name;
}
}
}
if (match.length() == 0) {
System.out.println("No Match!");
} else {
System.out.println(name);
}
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
String longest = arr[0][0];
// Use nested for loops to find the longest or shortest string!
System.out.println("Use nested for loops to find the longest or shortest string!");
int length = 0;
int max = 0;
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[row].length; col++) {
length = arr[row][col].length();
if (length > max) {
max = length;
longest = arr[row][col];
}
}
}
System.out.println(longest);
}
}
Test.main(null);
HW!
Google Form, which also has a few 2D array related multiple choice questions.
Please submit screenshots of your 4 hacks up and running to our
2017 FRQ Q4: Successor Array
in that same google form.
Additionally, Complete and send a screenshot of your code for Please submit as a pair or group.
public static Position findPosition(int num, int[][] intArr) {
for (int row = 0; row < intArr.length; row++) {
for (int col = 0; col < intArr[row].length; col++) {
if (intArr[row][col] == num) {
return
}
}
}
return null;
}
public static Position findPosition(int num, int[][] intArr) {
for (int row = 0; row < intArr.length; row++) {
for (int col = 0; col < intArr[row].length; col++) {
if (intArr[row][col] == num) {
return new Position(row, col); // correction
}
}
}
return null;
}
public static Position[][] getSuccessorArray(int[][] intArr) {
Position[][] arr = {};
for (int row = 0; row < intArr.length; row++) {
for (int col = 0; col < intArr[row].length; col++) {
arr[row][col] = findPosition(intArr[row][col] + 1, intArr);
}
}
return arr;
}
Comments
I graded my first attempt a 3/4 because I did not create the 2D array correctly. Since the successor array has the same dimension as the integer array, the dimensions of the successor array needed to be specified with Position[][] arr = new Position[intArr.length][intArr[0].length];
. Below is my correction.
public static Position[][] getSuccessorArray(int[][] intArr) {
Position[][] arr = new Position[intArr.length][intArr[0].length]; // correction
for (int row = 0; row < intArr.length; row++) {
for (int col = 0; col < intArr[row].length; col++) {
arr[row][col] = findPosition(intArr[row][col] + 1, intArr);
}
}
return arr;
}
int height = 3;
int spaceCounter = 0;
int leafCounter = 0;
int trunkSpace = height - 1;
String[][] tree = new String[height][2*(2*height-1)-1];
for (int i = 0; i < tree.length; i++) {
leafCounter = 0;
int maxLeaf = i * 2 + 1;
for (int j = 0; j < tree[i].length; j++) {
if (j < (2*(height - 1 - i))) {
tree[i][j] = " ";
} else if (j % 2 == 0 && leafCounter < maxLeaf) {
tree[i][j] = "*";
leafCounter++;
} else if (j % 2 != 0 && leafCounter < maxLeaf) {
tree[i][j] = " ";
} else {
tree[i][j] = " ";
}
}
}
for (int i = 0; i < tree.length; i++) {
for (int j = 0; j < tree[i].length; j++) {
System.out.print(tree[i][j]);
}
System.out.println();
}
import java.util.Scanner;
Scanner input = new Scanner(System.in);
System.out.println("How tall would you like your tree to be?");
int height = input.nextInt();
System.out.println(height);
int leafCounter = 0;
int trunkSpace = height - 1;
String[][] tree = new String[height + height - 1][2*(2*height-1)-1];
for (int i = 0; i < tree.length - (height-1); i++) {
leafCounter = 0;
int maxLeaf = i * 2 + 1;
for (int j = 0; j < tree[i].length; j++) {
if (j < (2*(height - 1 - i))) {
tree[i][j] = " ";
} else if (j % 2 == 0 && leafCounter < maxLeaf) {
tree[i][j] = "*";
leafCounter++;
} else {
tree[i][j] = " ";
}
}
}
for (int i = tree.length - (height-1); i < tree.length; i++) {
int maxLeaf = 3;
leafCounter = 0;
for (int j = 0; j < tree[i].length; j++) {
if (j < 2 * height - 1 -3) {
tree[i][j] = " ";
} else if (j % 2 == 0 && leafCounter < maxLeaf) {
tree[i][j] = "*";
leafCounter++;
} else {
tree[i][j] = " ";
}
}
}
for (int i = 0; i < tree.length; i++) {
for (int j = 0; j < tree[i].length; j++) {
System.out.print(tree[i][j]);
}
System.out.println();
}