Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » ArraysRSS Feeds

Multi dimensional array

Posted By: Rabiah Begum     Category: Java     Views: 12733

This article explains about multi dimensional arrays in java.

Definition of Multi Dimensional Array

Multi dimensional arrays are nothing but arrays of arrays.  You can create arrays of two or more dimensions.

Syntax of Multi dimensional Arrays

To create multi dimensional array, you need to perform two steps :

1) Declare multi dimensional array :

To declare multi dimensional array below is a general form or syntax.

type varName[][];

Where type is valid data type in java and varName is a name of an array. Note that the two sets of brackets indicates that there are two dimensions for this array.  

Example :      int iarr[][];
 

2) Allocate space for its elements :

To allocate space for multi dimensional array element use below general form or syntax.

varName = new type[size1][size2];

Where varName is the name of the array and type is a valid java type and dimensions are specified by size1 and size2 in the array.

Example :      iarr = new int[2][3];

Above statement will create 2*3 element array of an integer values that can by accessed by iarr.

Structure of multi dimensional array

 iarr[0][0]

  iarr[0][1]

  iarr[0][2] 

 iarr[1][0]

  iarr[1][1] 

  iarr[1][2]



Note that array indexes begins with zero. That means if you want to access 1st element of an array use zero as an index. To access 3rd element refer below example.

iarr[0][2] = 10; // it assigns value 10 to the third element of an array.

java also allows an abbreviated syntax to declare an array. General form or syntax of is as shown below.

type varName[][] = {{v00, v01, v02}, {v10,v11,v12}};

Where varName is the name of the array and type is a valid data type and v00, v01 to v12 are the elements of an array. In this case new is not used and memory for the array is automatically provided.

Example :    int iarr = {{1,2,3},{4,5,6}};  

Example of Multi dimensional array

Example : This example shows how to declare initialize  and display multi dimensional array.

// Declaration of allocating memory to multi dimensional array
int iarr[][]  = new int[2][3];

// Initializing elements
iarr[0][0] = 1;
iarr[0][1] = 2;
iarr[0][2] = 3;
iarr[1][0] = 4;
iarr[1][1] = 5;
iarr[1][2] = 6;

//Display array elements
System.out.println(iarr[0][0]); 
System.out.println(iarr[0][1]); 
System.out.println(iarr[0][2]); 
System.out.println(iarr[1][0]); 
System.out.println(iarr[1][1]); 
System.out.println(iarr[1][2]); 

// Or Use for loop to display elements
 for (int i = 0; i < iarr.length; i = i + 1) 
{
       for(int j=0; j < iarr[i].length; j = j + 1)
      {
             System.out.print(iarr[i][j]);  
             System.out.print(" ");
       }
 }



 

  
Share: 


Didn't find what you were looking for? Find more on Multi dimensional array Or get search suggestion and latest updates.

Rabiah  Begum
Rabiah Begum author of Multi dimensional array is from Singapore.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!