Accessing and Updating Elements in a 2D Array

Important Tips to Remember:

•	Array indices start at 0: The first row and first column are at index 0.
•	Relative positions: Always subtract 1 to determine the correct index relative to the 1-based numbering.

Accessing Elements

To access an element in a 2D array, use the following format:

arr[row][column]

Updating Elements

To update an element in a 2D array, assign a new value using this format:

variable_name[row][column] = new_value

For example: If you want to change the value in the second row and third column, use:

array[1][2] = new_value

Popcorn Hack:

Write code to access the scores: 92, 70, 93 and change the last score to 100

public class Main {
    public static void main(String[] args) {
        // Example 2D array representing the scores
        int[][] scores = {
            {100, 85, 95, 96},  // Student0
            {98, 100, 100, 95}, // Student1
            {92, 100, 98, 100}, // Student2
            {100, 100, 97, 99}, // Student3
            {100, 100, 100, 70},// Student4
            {100, 100, 99, 98}, // Student5
            {100, 94, 100, 93}  // Student6
        };

        // Access scores
        int score92 = scores[2][0];  // Row 2, Column 0
        int score70 = scores[4][3]; // Row 4, Column 3
        int score93 = scores[6][3]; // Row 6, Column 3

        // Print accessed scores with positions
        System.out.println("Accessed Scores:");
        System.out.println("Score 92 at position [2][0]: " + score92);
        System.out.println("Score 70 at position [4][3]: " + score70);
        System.out.println("Score 93 at position [6][3]: " + score93);

        // Change the last score to 100
        scores[scores.length - 1][scores[0].length - 1] = 100; // Last row, last column

        // Print updated scores
        System.out.println("\nUpdated Scores:");
        for (int i = 0; i < scores.length; i++) {
            for (int j = 0; j < scores[i].length; j++) {
                System.out.print(scores[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Main.main(null);
Accessed Scores:
Score 92 at position [2][0]: 92
Score 70 at position [4][3]: 70
Score 93 at position [6][3]: 93

Updated Scores:
100 85 95 96 
98 100 100 95 
92 100 98 100 
100 100 97 99 
100 100 100 70 
100 100 99 98 
100 94 100 100 

Essential points:

  • When accessing elements at arr[first][second], the first index is used for rows, and the second index is used for columns
  • Square brackets [row][col] are used to access and modify an element of a 2d array.

HW:

image.png image-2.png

public class ArrayTester {

    public static boolean isLatin(int[][] square) {
        // check if the first row contains duplicates
        if (containsDuplicates(square[0])) {
            return false;
        }

        // check if all rows contain the same values as the first row
        for (int i = 1; i < square.length; i++) {
            if (!hasAllValues(square[0], square[i])) {
                return false;
            }
        }

        // check if all columns contain the same values as the first row
        for (int j = 0; j < square.length; j++) {
            int[] column = getColumn(square, j);
            if (!hasAllValues(square[0], column)) {
                return false;
            }
        }

        return true;
    }

    // asume these helper methods are implemented elsewhere
    public static boolean containsDuplicates(int[] arr) {
        // returns true if there are duplicate values in arr
        return false; 
    }

    public static boolean hasAllValues(int[] arr1, int[] arr2) {
        // returns true if all values in arr1 are present in arr2
        return false; 
    }

    public static int[] getColumn(int[][] arr2D, int c) {
        // returns the c-th column of the 2D array arr2D
        return new int[0]; 
    }
}