Two Dimension Arrays in Java

The Java Program is 2D array is organized as matrices which can be represented as the collection of rows and column. It is possible to define an array with more than one dimension. Instead of being accessed by providing a single index, a multidimensional array is accessed by specifying an index for each dimension.

The declaration of multidimensional array can be done by adding [] for each dimension to a regular array declaration. For instance, to make a 2-dimensional int array, add another set of brackets to the declaration, such as int[][]. This continues for 3-dimensional arrays (int[][][]) and so forth.

     Datatype variable_name [ ] [ ] ;
     Datatype [ ][ ] variable_name ;

This program demonstrates the use of two-dimensional arrays in Java. It creates a 3x3 integer array named a with some initial values, and then prints its elements using nested for loops.

The outer loop iterates through the rows of the array (i), while the inner loop iterates through the columns of each row (j). The System.out.print() method is used to print each element with a space separator, and the System.out.println() method is called after each row to print a newline character.

The program then declares two more arrays: a 2D array b with dimensions of 10x10, and a 3D array c with dimensions of 10x10x10. The program assigns the value 10 to the first element of b using the syntax b[0][0] = 10.

Note that arrays in Java are zero-indexed, meaning that the first element has an index of 0, not 1. Therefore, the first row of a can be accessed using a[0], and the first element of a can be accessed using a[0][0].

Source Code

public class Two_Array {
    public static void main(String args[]) {
        //Two Dimension array in Java
        int a[][] = {
                {10, 20, 30},
                {40, 50, 60},
                {70, 80, 90}
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(" "+a[i][j]);
        //Array Declaration
        int [][]b=new int[10][10];
        int [][][]c=new int[10][10][10];


 10 20 30
 40 50 60
 70 80 90
To download raw file Click Here

Basic Programs