Unit8.3
Accessing and updating the values of a 2D array
In Java, accessing and updating values in a 2D array is done using the row and column indices. The general format is:
- **Accessing a value**: array[row][column]
- **Updating a value**: array[row][column] = newValue;
Popcorn Hack 1 (Part 2)
- Update the values of the array, you made in part 1 to the group members in another group
public class Main {
public static void main(String[] args) {
// the array from part 1
String[][] pairs = {
{"Sri", "Saathvik"},
{"Aidan", "Tanav"}
};
// the update
pairs[0] = new String[]{"Jonathan", "Tarun"};
pairs[1] = new String[]{"Ian", "Srijan"};
System.out.println("the updated pairs:");
for (int i = 0; i < pairs.length; i++) {
for (int j = 0; j < pairs[i].length; j++) {
System.out.print(pairs[i][j] + " ");
}
System.out.println();
}
}
}