Array

Two Dimensional Array in Java language

Two Dimensional Array in Java language

We will get to know about Two Dimensional Array in Java programming language in this tutorial.

In the Java Programming Language, an array is a fixed sequential collection of elements of the same data types. An array can be used to represent a list of numbers(int) or names (string) or other data type of similar elements. It is one of the ways of grouping similar type of data under single- variable name.

Three type of arrays in Java programming language.

1.  One dimensional array

2. Two-dimensional array

3. Three-dimensional array

 

Two-dimensional array in java

Typically, the two-dimensional array is an array data structure and it is one of the types of the multi-dimensional array in the Java programming language. Two dimensional array may contain data types such as int[][] or float[][]  or string[][] with two pairs of square brackets. The elements of the two-dimensional array are arranged in rows and columns as follows.

Two Dimension Array in Java

Declaration

Syntax

Two dimensional Array can be declared as…

data_Type[][] array_name;  ------------ 1  or
data_Type [][]array_name;  ------------ 2 or
data_Type array_name [][];  ------------ 3  or
data_Type []array_name[] ;  ------------ 4

 

Example

Example for Two dimension Array declaration

int [][]number;

Creation of two dimension Array

Syntax

data_type array_name=new data_type[row index][column index];

Example

int a=new int[3][4];// 3 row and 4 column

Initiation of two dimension array

We can initiate two-dimensional array in a various way. This method is initiated using one element. we put an element in every cell address.

Method 1

Syntax

array_name[row index][column index]=value;

Example

a[0][0]=45; //initialized first cell address

 

Example for initiation

array[0][0]=97;                //this cell address is  array[0][0]

array[0][1]=17;                 //this cell address is  array[0][1]

array[0][2]=27;               //this cell address is  array[0][2]

array[0][3]=93;               //this cell address is  array[0][3]

array[1][0]=65;               //this cell address is  array[1][0]

array[1][1]=87;              //this cell address is  array[1][1]

array[1][2]=90;             //this cell address is  array[1][2]

array[1][3]=27;            //this cell address is  array[1][3]

array[2][0]=45;               //this cell address is  array[2][0]

array[2][1]=86;              //this cell address is  array[2][1]

array[2][2]=80;             //this cell address is  array[2][2]

array[2][3]=77;            //this cell address is  array[2][3]

 

Memory representation of above two dimension Array program

Memory representation of 2 D array

Method 2

int[][] marks={                  //3 rows 4 coloum

{56,76,95,45},                    //row 1

{42,65,37,58},                   //row 2

{87,32,64,98},                  //row 3

};

 

The default initial value of  2 – dim array  in Java

When we declare an array, memory allocates space to store element and it is assigned to every cell by default initial value 0 for numeric, null for String, false for boolean etc.

We will learn deeply another chapter

class Default_Array{
public static void main(String args[]){
int age[][]=new int[2][3];  //2 d array declaration
for(int i=0; i<=1; i++){
    for(int j=0; j<=2; j++){
        System.out.println(age[i][j]);
                      }
                  }
              }
      }

When the above code is compiled and executed, it will produce the following results

0
0
0
0
0
0

 

How to display the array elements from the Two-dimensional array in Java

Accessing array elements

Display Array element in the 2d array

System.out.print(Array_name[column index][row index]);

Program 1

class twodarrayex{
public static void main(String args[])
{
int[][]marks; //declaration of array
marks=new int[2][3];//initiation of array
//initiate elements
marks[0][0]=25;
marks[0][1]=15;
marks[0][2]=20;
marks[1][0]=54;
marks[1][1]=10;
marks[1][2]=30;
//Display elements from array
System.out.println(marks[0][0]);
System.out.println(marks[0][1]);
System.out.println(marks[0][2]);
System.out.println(marks[1][0]);
System.out.println(marks[1][1]);
System.out.println(marks[1][2]);
}
}

 

When the above code is compiled and executed, it will produce the following results

25
15
20
54
10
30

Program

class TwoDarrayex1{
public static void main(String args[]){
int[][] array  =
{ //array declaration and initiation
{63,43,54,32}, 
{45,92,31,27},
{54,71,69,62},
};
//display elements
System.out.println(array[0][0]);
System.out.println(array[0][1]);
System.out.println(array[0][2]);
System.out.println(array[0][3]);
System.out.println(array[1][0]);
System.out.println(array[1][1]);
System.out.println(array[1][2]);
System.out.println(array[1][3]);
System.out.println(array[2][0]);
System.out.println(array[2][1]);
System.out.println(array[2][2]);
System.out.println(array[2][3]);

}
}

When the above code is compiled and executed, it will produce the following results

63
43
54
32
45
92
31
27
54
71
69
62
 

Program 1

class twodimArray{
public static void main (String args[]){
int[][] anArray;
anArray=new int[2][6];       // array declaration 
// array initiation
anArray[0][0]=124;           
anArray[0][1]=24;
anArray[0][2]=14;
anArray[0][3]=12;
anArray[0][4]=104;
anArray[0][5]=120;
anArray[1][0]=129;
anArray[1][1]=195;
anArray[1][2]=145;
anArray[1][3]=674;
anArray[1][4]=145;
anArray[1][5]=624;

//display array element
System.out.println("Element at index[0][0] :"+anArray[0][0]);
System.out.println("Element at index[0][1] :"+anArray[0][1]);
System.out.println("Element at index[0][2] :"+anArray[0][2]);
System.out.println("Element at index[0][3] :"+anArray[0][3]);
System.out.println("Element at index[0][4] :"+anArray[0][4]);
System.out.println("Element at index[0][5] :"+anArray[0][5]);
System.out.println("Element at index[1][0] :"+anArray[1][0]);
System.out.println("Element at index[1][1] :"+anArray[1][1]);
System.out.println("Element at index[1][2] :"+anArray[1][2]);
System.out.println("Element at index[1][3] :"+anArray[1][3]);
System.out.println("Element at index[1][4] :"+anArray[1][4]);
System.out.println("Element at index[1][5] :"+anArray[1][5]);
}
}

When the above code is compiled and executed, it will produce the following results

Element at index[0][0] : 124
Element at index[0][1] : 24
Element at index[0][2] : 14
Element at index[0][3] : 12
Element at index[0][4] : 104
Element at index[0][5] : 120
Element at index[1][0] : 129
Element at index[1][1] : 195
Element at index[1][2] : 145
Element at index[1][3] : 674
Element at index[1][4] : 145
Element at index[1][5] : 624

 

Access array elements using for loop in Java

We can print elements of all cells’ address in the two-dimensional array using nested for loop  based in the index

for(int i=0; i<=1; i++){
for(int j=0; j<=5; j++){
System.out.println(array_name[i][j]);
}
}
class array2d{
public static void main (String args[]){
int[][] anArray;
anArray=new int[2][6];
anArray[0][0]=345;
anArray[0][1]=145;
anArray[0][2]=365;
anArray[0][3]=349;
anArray[0][4]=355;
anArray[0][5]=745;
anArray[1][0]=340;
anArray[1][1]=315;
anArray[1][2]=445;
anArray[1][3]=344;
anArray[1][4]=375;
anArray[1][5]=645;

for(int i=0; i<=anArray.length-1; i++){
    for(int j=0; j<=anArray.length-1; j++){
    System.out.println(anArray[i][j]);
}
    }
        }

            }

 

When the above code is compiled and executed, it will produce the following results

345
145
365
349
355
745
340
315
445
344
375
645

 

Access element in two dimension Array using enhanced for loop

We can print elements of all cells’ address in the two-dimensional array using nested enhanced for loop – based in an index

for(int[]:array_name){
for(int j:i){
System.out.println(j)
}
}

Program 1

class array_2d{
public static void main (String args[]){
int x[][]=new int[3][3];
x[0][0]=305;
x[0][1]=320;
x[0][2]=133;
x[1][0]=355;
x[1][1]=460;
x[1][2]=666;
x[2][0]=365;
x[2][1]=530;
x[2][2]=533;



for(int i[]:x){
    for(int j:i){
    System.out.println(j);
}
}
}

}

 

When the above code is compiled and executed, it will produce the following results

305
320
133
355
420
666
365
530
533

 

Access element in two dimension Array using while loop

We can print elements of all cells’ address in the two-dimensional array using nested While loop in Java  based on the index

while(i<=1){
while(j<=2){
System.out.println(array_name[i][j]);
}
}

Program 1

class TwoD_array_while{
public static void main(String args[]){
int[][] array;  //array declaration
array=new int[2][3]; //array initiation
array[0][0]=967; //Assign value to array
array[0][1]=157;                
array[0][2]=247;          
array[1][0]=933;         
array[1][1]=625;             
array[1][2]=897;             

int i=0;
while(i<=1){//outer while loop
    int j=0;
    while(j<=2){//inner while loop
    System.out.println(array[i][j]);
    j++;
            }
        i++;
        }
    
    }

}

 

When the above code is compiled and executed, it will produce the following results

967
157
247
933
625
897

 

Similar post

Two dimension Array in C++ language

Three dimension Array in C++ language

Single dimension Array in Java language

Two dimension Array in Java language

Three dimension Array in Java language

Single dimension Array in C language

Two dimension Array in C language

Three dimension Array in C language

 

Suggested for you

for loop in C++ language

While loop in C++ language

Nested for loop in C++ language

Nested while loop in C++ language

New keyword in Java language

For loop in Java language

While loop in Java language

Nested for loop in Java language

Nested while loop in Java language

Single dimension Array in Cpp language
Three Dimension Array in Java Language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago