Home Java Beginners Arrayexamples Two dimensional array in java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Two dimensional array in java

Advertisement
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". 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

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 7, 2013

Ask Questions?    Discuss: Two dimensional array in java.  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com