Two dimensional array in java

In this section you will learn about two-dimensional array in java with an example. As we know that array is a collection of similar data type or it can hold fixed number of value of same type. Two dimensional array is defined as an array of array.

Two dimensional array in java

In this section you will learn about two-dimensional array in java with an example. As we know that array is a collection of similar data type or it can hold fixed number of value of same type. Two dimensional array is defined as an array of array.

Two dimensional array in java.

Two dimensional array in java.

In this section you will learn about two-dimensional array in java with an example. As we know that array is a collection of similar data type or it can hold fixed number of value of same type. Two dimensional array is defined as an "array of array". In java the element of array can be of any type object. When we create array then length will be established, it is fixed when created.In two dimension array data are represented with rows and column. each item of an array is called element, to access the element numerical index called subscript is used .

Declaring an array: To declare an array just two square bracket with empty list required as follows.

int arr[][];

Here arr is a two-dimensional array of type int. To create an array we have to use new keyword, and give the length of each bracket.

arr = new int[10][10];

Here, first subscript indicate that arr array has 10 element, and second subscript indicate that each of those element is itself an array with 10 element.

To process the two-dimension array, for loop is used .

Example : A simple program of two dimension array.

public class Twodarray {

    public static void main(String args[]){

       int a[][]=new int[5][4]; //An array with 5 row and 4 coloumn.
       for(int i=0;i<a.length;i++)  
       {
           for(int j=0;j<a[i].length;j++)
            {
           a[i][j]=i;
           System.out.print(" " +a[i][j]);
              }

           System.out.println(" ");
          }
        }
       }

In the above example declaring an array a of size with five rows and four columns indicating that it has 5 array of int, each of those array element has 4 array .

Output: After Compiling and executing of above program.

Download SourceCode