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

Accessing and Updating Elements of a 2D Array:

  • nameOf2DArray[r][c]

Hack 1: Access the last element of the 2D Array list:

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);
i

Hack 2: Changing a 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!");
      
       if(arr[2][0] == "Austin") {
         arr[2][0] = "Athens";
       }

       System.out.println(arr[2][0]);
    }
 
 }
 Test.main(null);
Change Austin to Athens and print!
Athens

Nested Loops:

  • to print out an array, use nested loop which is a for loop within another
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);
a f g l  
b e h k  
c d i j  

Hack 3: Unknown Dimensions:

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);
Atlanta Baltimore Chicago  
Australia Boston Cincinnati  
Austin Beaumont Columbus  

Searching for a Value in a 2D Array:

enhanced for loop

  • used to traverse through 2d arrays or collections, can only go up by 1 and only forward ### .equals method
  • checks if something is equal to another
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);
Boston

Hack 4

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);
Longest string: Cincinnati

HW FRQ

4a

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;
}

4b

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;
}