Unit 8 - 2D Arrays
Notes, Hacks, and HW for 2D Arrays.
- Some Vocab:
- Accessing and Updating Elements of a 2D Array:
- Nested Loops:
- Searching for a Value in a 2D Array:
- HW FRQ
Some 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
- ## 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
-
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "a", "f", "g" },
{ "b", "e", "h" },
{ "c", "d", "i" }
};
System.out.print(arr[2][2]);
// Print the last element in the array!
}
}
Test.main(null);
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!");
if(arr[2][0] == "Austin") {
arr[2][0] = "Athens";
}
System.out.println(arr[2][0]);
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = { //making a 2d array
{ "a", "f", "g", "l" },
{ "b", "e", "h", "k" },
{ "c", "d", "i", "j" }
};
for (int row = 0; row < 3; row++) { //nested for loop here, where the columns are printed by rows
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" }
};
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
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 each or enhanced loop used
for (String item : row) {
if (item.equals(name)) { //.equals method to check if item is equal to 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];
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr[i].length; j++) { //loops through with nested for loop
if (arr[i][j].length() > longest.length()) { //checks length if word, if longer then it replaces
longest = arr[i][j];
}
}
}
System.out.println("Longest string: " + longest);
}
}
Test.main(null);
public static Position findPosition(int num, int[][] intArr)
{
for(int r = 0; r < intArr.length; r++)
for(int c = 0; c < intArr[0].length; c++)
if(intArr[r][c] == num)
return new Position(r, c);
return null;
}
public static Position[][] getSuccessorArray(int[][] intArr)
{
Position[][] successors = new Position[intArr.length][intArr[0].length];
for(int r = 0; r < successors.length; r++)
for(int c = 0; c < successors[0].length; c++)
successors[r][c] = findPosition(intArr[r][c] + 1, intArr);
return successors;
}