OneCompiler

2D ARRAY JAVA

1618

import java.util.*;

public class Main {
public static void main(String[] args) {
int i, j;

    // Initialize the 2D array
    int a[][] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    System.out.println("Data of 2D array in matrix form:");

    // Loop through the rows
    for (i = 0; i < 3; i++) { // Change to 3 for a 3x3 array
        // Loop through the columns
        for (j = 0; j < 3; j++) {
            System.out.print(a[i][j] + " "); // Print with a space
        }
        System.out.println(); // Move to the next line after each row
    }
}

}