• Declaring and Initializing 2D Arrays in Java
  • Typing Speed Test
  • What is a 2D array?

    Vocabulary


    Array

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

      2D Array

    • Arrays of arrays, representing collections of related primitive or object reference data.
    • Created and indexed similar to 1D array

      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.

    Examples


    • 1D Array: Roster of students with strings.

    Image 1

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

    Image 1

    • This screenshot shows:
      • Collection of arrays representing student grades, where each entry is an array of a student’s grades.
      • A rectangular 2D array since each row array has the same number of entries.
      • 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];
        • All the values in this array are zero
    • If you want to actually put values inside your array (initialize):
      • Use { }
      • Each row of a 2D array has its own initializer list
      • Separate elements of an array with a comma, same goes for separating a row array from another row array

    Example:

    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
    
    Typing Test

    Typing Speed Test

    Type this sentence: int[][] Name = new datatype[rows][columns]

    Popcorn Hack!

    What’s wrong with this code:

    int[][] exampleArray = {
        {"Hello", "World"},
        {"Java", "Array"}
    };
    

    Popcorn Hack!

    How many total elements can this 2D array hold, and how would you calculate it?

    int[][] matrix = new int[2][3];
    

    Popcorn Hack!

    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

    Try on your own!

    Write a code to declare and initialize a 2D array that stores the following matrix: 1 2 3 4 5 6 7 8 9

    public class ArrayPractice {
        public static void main(String[] args) {
            // Write the code to declare and initialize the 2D array here
    
            // Print the array
            System.out.println(java.util.Arrays.deepToString(array));
        }
    }
    
    ArrayPractice.main(null)
    

    Updating elements in arrays

    • Identify the position (row and column) of the element you want to update.
    • Use the array name followed by the index of the row and the index of the column inside square brackets.
    • Assign a new value to that specific position.
    • Example:
    int[][] matrix = {
        {1, 2, 3},
        {4, 5, 6}
    };
    
    // To update the element in the first row, second column
    matrix[0][1] = 10;
    

    Try on your own!

    Write the code to change the element “Java” to “Programming”

    String[][] hack = {
        {"Hello", "World"},
        {"Java", "Array"}
    };
    
    //Insert code below:
    

    Popcorn Hack!

    Image 1

    Usage of 2D arrays?

    • Collegeboard is stinky and are on the CSA test every year
      • Collegeboard tries to mess you up on 2D arrays!
        • They will me around with column order, nonunified arrays, and might throw garbage into the array like putting string into a numeric array. BE CAREFUL!
    • 2D array biggest application is managing images
      • They represent images as grids of pixels, where each element corresponds to a pixel’s color or intensity
      • Each pixel is a single value (0 to 255) indicating brightness.