Learning Objective!

Represent collections of related primitive and object reference data using two dimensional array objects.

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

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);
arr[0][0] = 1
arr[1][2] = 6
arr[2][1] = 8

Accessing and Updating Elements of a 2D Array:

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.println(arr[arr.length - 1][arr[0].length - 1]);
    }
 
 }
 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" }
      };
 
      arr[2][0]  = "Athens";
      System.out.println(arr[2][0]);
       
    }
 
 }
 Test.main(null);
Athens

Nested Loops, Our Beloved:

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);
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 row = 0; row < 3; row++) {
          for (int col = 0; col < 3; col++) {
             System.out.print(arr[row][col] + " ");
          }
         System.out.println(" ");
       }
        
       
    }
 
 }
 Test.main(null);
Atlanta Baltimore Chicago  
Australia Boston Cincinnati  
Austin Beaumont Columbus  

Searching for a Value in a 2D Array:

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

Hack 4: Finding the Min/Max Value:

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++){
            if (longest.length() < arr[i][j].length()) {
              longest = arr[i][j]; 
            }
          }
        }
        System.out.println(longest);
        
        String shortest = arr[0][0];
        for (int i = 0; i < arr.length; i++){
          for (int j = 0; j < arr[i].length; j++){
            if (shortest.length() > arr[i][j].length()) {
              shortest = arr[i][j]; 
            }
          }
        }
        System.out.println(shortest);
    }
 
 }
Test.main(null);
Cincinnati
Boston

HW!

Please submit screenshots of your 4 hacks up and running to our Google Form, which also has a few 2D array related multiple choice questions. Additionally, Complete and send a screenshot of your code for 2017 FRQ Q4: Successor Array in that same google form. Please submit as a pair or group.

2017 FRQ - Part A

public static Position findPosition(int num, int[][] intArr)
{
    for (int row=0; row < intArray.length; row++)
    {
        for (int col=0; col < intArray[0].length; col++)
        {
            if (intArray[row][col] == num)
            {
                return new Position(rol, col);
            }
        }
    }
    return.null;
}

2017 FRQ - Part B

public static Position[][] getSuccessorArray(int[][] intArr)
{
    Position[][] newArray = new Position[intArray.length][intArray[0].length];

    for (int row=0; row < intArray.length; row++)
    {
        for (int col=0; col < intArray[0].length; col++)
        {
        newArray[row][col] = findPosition(intArray[row][col]+1, intArray);
        }
    }
    return newArray;

}

Christmas Tree EC

public class ChristmasTree {

    public static void main(String[] args) {
 
      String[][] tree = {
         { " ", " ", " ", " ", "*", " ", " ", " ", " "},
         { " ", " ", " ", "*", "*", "*", " ", " ", " "},
         { " ", " ", "*", "*", "*", "*", "*", " ", " "},
         { " ", " ", " ", "*", "*", "*", " ", " ", " "},
         { " ", " ", "*", "*", "*", "*", "*", " ", " "},
         { " ", "*", "*", "*", "*", "*", "*", "*", " "},
         { " ", " ", "*", "*", "*", "*", "*", " ", " "},
         { " ", "*", "*", "*", "*", "*", "*", "*", " "},
         { "*", "*", "*", "*", "*", "*", "*", "*", "*"},
      };
 
      // Print the last element in the array!
      for (String[] row : tree) {
         for (String item : row) {
             System.out.print(item + " ");
         }
         System.out.println(' ');
     }
    }
 
 }
ChristmasTree.main(null);
        *          
      * * *        
    * * * * *      
      * * *        
    * * * * *      
  * * * * * * *    
    * * * * *      
  * * * * * * *    
* * * * * * * * *