In Java, you can declare a multidimensional array using the following syntax:
dataType[][] arrayName = new dataType[outerSize][innerSize];
Here’s an example of how to declare and initialize a 2D array of integers in Java JDK 22:
public class Main { public static void main(String[] args) { // Declare and initialize a 2D array of integers int[][] twoDArray = new int[3][4]; // Assign values to the elements of the array twoDArray[0][0] = 1; twoDArray[0][1] = 2; twoDArray[0][2] = 3; twoDArray[0][3] = 4; twoDArray[1][0] = 5; twoDArray[1][1] = 6; twoDArray[1][2] = 7; twoDArray[1][3] = 8; twoDArray[2][0] = 9; twoDArray[2][1] = 10; twoDArray[2][2] = 11; twoDArray[2][3] = 12; // Print the elements of the 2D array for (int i = 0; i < twoDArray.length; i++) { for (int j = 0; j < twoDArray[i].length; j++) { System.out.print(twoDArray[i][j] + " "); } System.out.println(); } } }
In this example, `twoDArray` is a 2D array with 3 rows and 4 columns. We initialize it with zeroes, and then assign values to each element manually. Finally, we print the elements of the array using nested loops.