Home / Questions / Declare a 2D array of integers in Java with 3 rows and 4 columns. Initialize the array with the following values: 1 2 3 4 5 6 7 8 9 10 11 12
Explanatory Question

Declare a 2D array of integers in Java with 3 rows and 4 columns. Initialize the array with the following values:


1  2  3  4  
5  6  7  8  
9  10  11  12

👁 51 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

Write a Java program to print all the elements of the array.:


public class Main {
    public static void main(String[] args) {
        int[][] array = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };
        
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
}