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.

  Syntax:
     Datatype variable_name [ ] [ ] ;
     (or)
     Datatype [ ][ ] variable_name ;

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]);
            }
            System.out.println("");
        }
        //Array Declaration
        int [][]b=new int[10][10];
        int [][][]c=new int[10][10][10];
        b[0][0]=10;
 
    }
}
 

Output

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

Basic Programs