• Vocabulary
  • Declaring and Initializing 2D Arrays in Java
  • Popcorn Hack #1
  • Learning Objectives:
  • 8.1 Info

    Main Concepts

    Representing Collections with 2D Arrays

    • 2D arrays: Arrays of arrays, representing collections of related primitive or object reference data.
    • Creation and Indexing: Created and indexed similarly to 1D arrays, but each element is an array.
    • Initializer Arrays: Can be initialized using initializer lists, which represent 1D arrays.

    Vocabulary

    Array

    • A data structure used to implement a list of primitive or object reference data.

    Element

    • A single value in an array.

    Index

    • The position of an element in an array. The first element is at index 0 in Java.

    Length of an Array

    • The number of elements in the array. It’s a public final data member of an array.
      • Public: Accessible in any class.
      • Final: The length of the array cannot change after it is created.
      • The last element of an array is the length of the index - 1.

    Example

    • 1D Array: Roster of students with strings.

    Image 1

    • 2D Array: Student grades (array of arrays).

    Image 1

    • A student’s grades:
      • Collection of arrays representing student grades, where each entry is an array of a student’s grades.
      • This is a rectangular 2D array since each row array has the same number of entries.
      • Example: 7 arrays (students), each with 4 grades (tests). Total: 28 elements.

    Declaring and Initializing 2D Arrays in Java

    Declaring Arrays

    To declare a 2D array in Java, use the following format:

    int[][] 2DArrayName = new datatype[# of rows][# of columns]

    Example

    int[][] grades = new int[3][4];

    public class Main {
        public static void main(String[] args) {
            int[][] grades = {
                {90, 85, 88, 92}, 
                {75, 80, 78, 85},
                {60, 65, 70, 75}
            };
    
            int rows = grades.length; // Attribute 1
            int columns = grades[0].length;
    
            System.out.println("Number of rows: " + rows);
            System.out.println("Number of columns: " + columns);
    
            int element = grades[1][2]; // Accessing an element 
            System.out.println("Element at row 2, column 3: " + element);
        }
    }
    
    Main.main(null)
    
    Number of rows: 3
    Number of columns: 4
    Element at row 2, column 3: 78
    

    Popcorn Hack #1

    Image 1

    How would you write the code to access the last score? MC

    • A. grades[6][3]
    • B. grades[7][4]
    • C. grades[grades.length - 1] [grades[0].length - 1]
    • D. A and C

    Learning Objectives:

    • Traverse 2D arrays using nested for loops.
    • Traverse 2D arrays using nested enhanced for loops.

    Needed Knowledge:

    • Nested iteration refers to statements that iterate over elements in a 2D array.
    • 2D arrays are arrays of arrays and are traversed using for loops, similar to enhanced for loops for 1D arrays.

    Key Concept:

    • Nested iteration statements can be written to traverse the 2D array in row-major order or column-major order.

    Traversing with Nested Enhanced For Loops:

    • The outer loop in a nested enhanced for loop traverses the rows.
    • The enhanced for loop variable must be a 1D array type since the row itself is a 1D array.
    • The inner loop traverses a single row, and the inner enhanced for loop variable must be the same type as the elements in the 1D array.

    Example Code: Nested For Loops

    public class NestedLoops {
        public static void main(String[] args) {
            for (int outer = 1; outer < 5; outer++) {
                for(int inner = 1; inner < 3; inner++) {
                    System.out.print(inner + " ");
                }
                System.out.println();
            }
        }
    }
    
    NestedLoops.main(null);
    
    1 2 
    1 2 
    1 2 
    1 2 
    

    How this applies to 2D Arrays:

    public class Main {
        public static void main(String[] args) {
            String[][] grid = new String[4][5];
            for (int row = 0; row < 4; row++) {
                for (int col = 0; col < 5; col++) {
                    System.out.print(grid[row][col] + " "); // Look at the Double For loops, this is used for 2D-arrays, not 1D
                }
            System.out.println();
            }
        }
    }
    
    Main.main(null);
    
    null null null null null 
    null null null null null 
    null null null null null 
    null null null null null 
    

    Popcorn Hack 2

    Image 1

    • Reference code below

    Image 1

    Row Order

    • Given the code below: Image 1

    Output is:

    Image 1

    • Another way of writing this is by using colons (:) Image 1

    Homework

    • Create a program using 2D-Arrays

    Requirements:

    • It must use row order
    • Use the length attributes to go through the double for loop
    • Create one program for integers and one using string