2D Arrays Study Notes

2D Arrays

Introduction to 2D Arrays

  • Definition: A 2D array, also referred to as a matrix, is an array of arrays.

    • Example Declaration: int[][] mat = new int[3][3];

      • Creates a 3x3 integer matrix initialized to zero:
        0 0 0 0 0 0 0 0 0

Declaration and Initialization

  • Example initialization of a 2D array:

    • int[][] mat = {{6, 9, 2}, {5, 3, 4, 6}};

      • The above example creates a 2D array called mat with different sized rows.

      • Representation:
        6 9 2 5 3 4 6

  • Other examples of declarations:

    • String[][] words = new String[2][4]; // 8 null strings

    • double[][] dec = new double[3][6]; // 18 zeros

    • int[][] mat = new int[5][11]; // 55 zeros

Accessing Values

  • Example code for accessing values in a 2D array:
    int[][] mat = {{5, 7, 9, 2, 1, 9}, {5, 3, 4}, {3, 7, 0, 8, 9}}; out.println(mat[2][1]); // OUTPUT: 7 out.println(mat[1][2]); // OUTPUT: 4 out.println(mat[0][3]); // OUTPUT: 2 out.println(mat[2][4]); // OUTPUT: 9

  • Further accessing examples:
    out.println(mat[7/4][0]); // OUTPUT: 5 out.println(mat[1*2][2]); // OUTPUT: 0 out.println(mat.length); // OUTPUT: 3 out.println(mat[0].length); // OUTPUT: 6

Changing Values

  • Changing a specific element in a 2D array: mat[0][1] = 2; // Set 1st row, 2nd column to 2 mat[2][2] = 7; // Set 3rd row, 3rd column to 7 mat[0][3] = 5; // Set 1st row, 4th column to 5 mat[4][1] = 3; // Out of bounds (incorrect)

    • Attempting mat[4][1] would result in an ArrayIndexOutOfBoundsException.

Traversing 2D Arrays

  • Example of traversing a 2D array and printing:
    for(int r = 0; r < mat.length; r++) { for(int c = 0; c < mat[r].length; c++) { out.print(mat[r][c]); } out.println(); }

  • Output from traversal:
    OUTPUT: 5 7 OUTPUT: 5 3 4 6 OUTPUT: 0 8 9

  • Alternative method of traversing using enhanced for loop:
    for(int[] row : mat) { for(int num : row) { System.out.print(num + " "); } System.out.println(); }

Searching in 2D Arrays

  • Counting occurrences of a specific integer in 2D arrays:
    int count = 0; for(int r = 0; r < mat.length; r++) { for(int c = 0; c < mat[r].length; c++) { if(mat[r][c] == 5) count++; } } System.out.println("5 count = " + count); // OUTPUT: 2

  • An enhanced method for searching across rows:
    count = 0; for(int[] row : mat) { for(int num : row) { if(num == 5) count++; } } System.out.println("5 count = " + count); // OUTPUT: 2

Summing Elements in a 2D Array

  • Summing all elements in a 2D array:
    int sum = 0; for(int[] row : mat) { for(int num : row) { sum += num; } } System.out.println(sum); // OUTPUT: 47

Example Problem using a 2D Array

  • Consider coding a segment using a 2D array of letters: String[][] letters = {{"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"}}; System.out.println( /* missing code */ );

    • Options to replace /* missing code */ must access letters in array format to print expected values, i.e., letters that yield "DIG".

Conclusion

  • Understanding 2D arrays is crucial for organizing data in grid formats.

  • Key operations include declaration, value access, modification, traversal, searching, and summing.

  • Applications span various domains, including mathematics, computer graphics, and data science.

Class Reference Example

  • Example of class with a 2D array of custom objects:
    public class Dog { private int age; private String name; public Dog(String n, int a) { age = a; name = n; } public int getAge() { return age; } public String getName() { return name; } public String toString() { return "Dog - " + name + " " + age; } } Dog[][] herd; herd = new Dog[3][3]; herd[0][0] = new Dog("fred", 11); herd[1][2] = new Dog("ann", 21); System.out.println(herd[2][2]); // OUTPUT: null System.out.println(herd[0][0]); // OUTPUT: Dog - fred 11

  • This example demonstrates instantiating and using 2D arrays to store objects.