Lesson 8 - 2D Array
- Vocabulary
- Hack 1: Access the last element of the 2D Array list:
- Hack 2: Changing a Value:
- Hack 3: Unknown Dimensions:
- Hack 4: Finding the Min/Max Value:
- Extra submission
Vocabulary
- 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
- A 2D array is an array of arrays, and can be a better way to store data
- Declaring a 2D array:
DataType[][] nameOf2DArray
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("last element: "+ arr[2][2]);
}
}
Test.main(null);
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!");
System.out.println(arr[arr.length - 1][arr[0].length - 3]);
arr[arr.length - 1][arr[0].length - 3] = "Athens";
System.out.println(arr[arr.length - 1][arr[0].length - 3]);
for(int row = 0; row < arr.length; row++) {
for(int column = 0; column < arr.length; column++) {
System.out.print(arr[row][column] + " ");
}
System.out.println(" ");
}
}
}
Test.main(null);
You can use Nested Loops to traverse 2D Arrays
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];
String shortest = 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!");
for(int row =0; row < arr.length; row++) {
for(int col=0; col < arr[row].length; col++) {
if (arr[row][col].length() > longest.length()) {
longest = arr[row][col];
}
}
}
System.out.println("Longest String: " + longest);
for(int row =0; row < arr.length; row++) {
for(int col=0; col < arr[row].length; col++) {
if (arr[row][col].length() < shortest.length()) {
shortest = arr[row][col];
}
}
}
System.out.println("Shortest String: " + shortest);
}
}
Test.main(null);
int height = 20;
String[][] arr = new String[height][height];
for (int col=0; col<=height; col+=2) {
System.out.println(" ".repeat(height - col/2) +"*".repeat(col));
}
System.out.println(" ".repeat(height - 1) + "*".repeat(2));
System.out.println(" ".repeat(height - 1) + "*".repeat(2));