Replace the “Add your code here// below to print the values of 2, 5 and 50 to access them from the given 2D array.
public class Array1
{
public static void main(String[] args)
{
int[][] array = { {1,2,3}, 5, {30,40,45}, 6, {50,60,70} };
// Add your code here //
}
}
Share
Answer:
Here's the updated code with the solution:
public class Array1 {
public static void main(String[] args) {
int[][] array = { {1,2,3}, {5}, {30,40,45}, {6}, {50,60,70} };
// Accessing the values of 2, 5, and 50 from the 2D array
int value1 = array[0][1]; // the value 2 is at index [0][1]
int value2 = array[1][0]; // the value 5 is at index [1][0]
int value3 = array[4][0]; // the value 50 is at index [4][0]
// Printing the values
System.out.println("The values are: " + value1 + ", " + value2 + ", " + value3);
}
}
In this solution, we first corrected the 2D array syntax by putting each inner array in curly braces. Then, we accessed the values of 2, 5, and 50 from the 2D array using their respective indexes, and assigned them to variables. Finally, we printed the values using System.out.println().