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

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[2][2] = " + arr[2][2]);
    }
 
 }
 Test.main(null);
arr[2][2] = i

Hack 2

public class Test {

    public static void main(String[] args) {
 
      String[][] arr = {
         { "Atlanta", "Baltimore", "Chicago" },
         { "Australia", "Boston", "Cincinnati" },
         { "Austin", "Beaumont", "Columbus" }
      };
      arr[2][0] = "Athens";
       // Change Austin to Athens and print!
      System.out.println("Change Austin to Athens and print!");
      
      System.out.println(arr[2][0]);
    }
 
 }
 Test.main(null);
Change Austin to Athens and print!
Athens

Nested Loop

  • nested for loop places one for loop inside another for loop
  • inner loop repeats for every iteration of the outer one
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" }
       };
       for (int row = 0; row < 3; row++) {
         for (int col = 0; col < 3; col++) {
            System.out.print(arr[row][col] + " ");
         }
        System.out.println(" ");
      }
       // Print out the array without using numerical values!
       
    }
 
 }
 Test.main(null);
Atlanta Baltimore Chicago  
Australia Boston Cincinnati  
Austin Beaumont Columbus  

Searching for a Value in 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

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 (String[] row : arr) {
            for (String item : row) {
                if (item.length()>longest.length()) {
                    longest = item;
                }
            }
        }
        // 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!: " + longest);
        
    }
 
 }
Test.main(null);
Use nested for loops to find the longest or shortest string!: Cincinnati

Homework

2017 FRQ 4

(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. For example, assume that array arr is the 2D integer array shown at the beginning of the question. • The call findPosition(8, arr) would return the Position object (2,1) because the value 8 appears in arr at row 2 and column 1. • The call findPosition(17, arr) would return null because the value 17 does not appear in arr.

/** Returns the position of num in intArr;
 * returns null if no such element exists in intArr.
 * Precondition: intArr contains at least one row.
*/ 

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

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

/** Returns a 2D successor array as described in part (b) constructed from intArr.
 * Precondition: intArr contains at least one row and contains consecutive values.
* Each of these integers may be in any position in the 2D array.
 */ 

 public static Position[][] getSuccessorArray(int[][] intArr) 
{
    Position[][] successor = new Position[intArr.length][intArr[0].length];
    for(int r=0;r<intArr.length;c++)
    {
        for(int c=0;c<intArr[0].length;c++)
        {
            Position p = findPosition(intArr[r][c]+1 , intArr);
            
        }
    }
}

Tree

int height = 3; 
int spaceCounter = 0;
int leafCounter = 0;
int trunkSpace = height - 1; 

String[][] tree = new String[height][2*(2*height-1)-1]; 

for (int i = 0; i < tree.length; i++) {
    leafCounter = 0; 
    int maxLeaf = i * 2 + 1; 
    for (int j = 0; j < tree[i].length; j++) {
        if (j < (2*(height - 1 - i))) {
            tree[i][j] = " ";
        } else if (j % 2 == 0 && leafCounter < maxLeaf) {
            tree[i][j] = "*";
            leafCounter++; 
        } else if (j % 2 != 0 && leafCounter < maxLeaf) {
            tree[i][j] = " ";
        } else {
            tree[i][j] = " "; 
        }

    }
}


    
for (int i = 0; i < tree.length; i++) {
    for (int j = 0; j < tree[i].length; j++) {
        System.out.print(tree[i][j]); 
    }
    System.out.println();
}
import java.util.Scanner;

Scanner input = new Scanner(System.in); 
System.out.println("How tall would you like your tree to be?"); 
int height = input.nextInt();

System.out.println(height);

int leafCounter = 0;
int trunkSpace = height - 1; 

String[][] tree = new String[height + height - 1][2*(2*height-1)-1]; 

for (int i = 0; i < tree.length - (height-1); i++) {
    leafCounter = 0; 
    int maxLeaf = i * 2 + 1; 
    for (int j = 0; j < tree[i].length; j++) {
        if (j < (2*(height - 1 - i))) {
            tree[i][j] = " ";
        } else if (j % 2 == 0 && leafCounter < maxLeaf) {
            tree[i][j] = "*";
            leafCounter++; 
        } else {
            tree[i][j] = " "; 
        }

    }
}



for (int i = tree.length - (height-1); i < tree.length; i++) {
    int maxLeaf = 3; 
    leafCounter = 0; 
    for (int j = 0; j < tree[i].length; j++) {
        if (j < 2 * height - 1 -3) {
            tree[i][j] = " "; 
        } else if (j % 2 == 0 && leafCounter < maxLeaf) {
            tree[i][j] = "*";
            leafCounter++;
        } else  {
            tree[i][j] = " "; 
        }

    }
}
    
for (int i = 0; i < tree.length; i++) {
    for (int j = 0; j < tree[i].length; j++) {
        System.out.print(tree[i][j]); 
    }
    System.out.println();
}
How tall would you like your tree to be?
5
        *        
      * * *      
    * * * * *    
  * * * * * * *  
* * * * * * * * *
      * * *      
      * * *      
      * * *      
      * * *