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

2D Array

  • an array of arrays
  • start with setting data type
  • initialize array
    • number of rows
    • if not known use r = list.length
    • number of columns
    • if not known use c = list[0].length

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

Hack 1

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[arr.length - 1][arr[0].length - 1]);
    }
 
 }
 Test.main(null);
i

Hack 2

  • use listName[r][c] = value; to update item
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!
       arr[2][0] = "Athens";
       
       System.out.println(arr[2][0]);
       
    }
 
 }
 Test.main(null);
Athens

Nested Loop

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

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 < 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 an Array

  • find a certain element within array
  • if item matches name, then print there is a match and print the match
  • (:) represents within the array/row/column
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

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!

    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

2017 FRQ Q4 Part A

Write a static method findPosition that takes an integer value and a 2D integer array and returns the position of the integer in the given 2D integer array. If the integer is not an element of the 2D integer array, the method returns null.

public static Position findPosition(int num, int[][] intArr){
    for (int row=0; row < intArr.length; row++){

        for (int col=0; col < intArr[0].length; col++){

            if (intArr[row][col] == num){
                return new Position(row, col);
            }
        }
    }
    return null;
}
|   public static Position findPosition(int num, int[][] intArr){
|       for (int row=0; row < intArr.length; row++){
|   
|           for (int col=0; col < intArr[0].length; col++){
|   
|               if (intArr[row][col] == num){
|                   return new Position(row, col);
|               }
|           }
|       }
|       return null;
|   }
Unresolved dependencies:
   - class Position

2017 FRQ Q4 Part B

Write a static method getSuccessorArray that returns a 2D successor array of positions created from a given 2D integer array.

The successor of an integer value is the integer that is one greater than that value. For example, the successor of 8 is 9. A 2D successor array shows the position of the successor of each element in a given 2D integer array. The 2D successor array has the same dimensions as the given 2D integer array. Each element in the 2D successor array is the position (row, column) of the corresponding 2D integer array element’s successor. The largest element in the 2D integer array does not have a successor in the 2D integer array, so its corresponding position in the 2D successor array is null.

The following diagram shows a 2D integer array and its corresponding 2D successor array. To illustrate the successor relationship, the values 8 and 9 in the 2D integer array are shaded. In the 2D successor array, the shaded element shows that the position of the successor of 8 is (0,2) in the 2D integer array. The largest value in the 2D integer array is 16, so its corresponding element in the 2D successor array is null.

public static Position[][] getSuccessorArray(int[][] intArr){
    Position[][] newArr = new Position[intArr.length][intArr[0].length];
    for (int row=0; row < intArr.length; row++){

        for (int col=0; col < intArr[0].length; col++){

            newArr[row][col] = findPosition(intArr[row][col]+1, intArr);

        }

    }

    return newArr;
}

Christmas Tree

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