Two Dimensional Array in Java language

Two Dimensional Array in Java

In this tutorial, we will discuss the Two Dimensional Array in Java language.

In this post, we will learn how to define the two-dimensional array and how to use it

To understand the two-dimensional array, we can be defined as the  appropriate words as an array of array

Two Dimensional Array

Declaration of two-dimensional array

Syntex
Two dim arrays can declare the following methods
dataType[][] arrayname;   ——  1   or
dataType [][]arrayname;   ——- 2   or
dataType arrayname [][];   —— 3    or

dataType []arrayname[];    —— 4

you can choose any one method
Example for Two dim array declaration
int [][] array;
Syntax

Creation of two-dimensional array

Syntax
datatype[row count][coloum count] arrayname=new datatype;
Example of Creation of Twodimensional array
array=new int[5][5];  //5 row  and  5 coloum
Declaration

The default value of array

Find the default value of the two-dimensional array after declaring a two-dimensional array
class ArrayWithForTwoDimdefault{
public static void main (String args[]){
int arr[][]=new int[4][3];//array declaration

System.out.print("\ndisplay cell value in an array: \n");
for(int i=0; i<4; i++){
   for(int j=0; j<3; j++){
      System.out.print("\ndisplay Element index["+i+"]["+j+"]:"+arr[i][j] );
}
}
}
}

When the above code is executed it produces the following result

Default value

initiation of Two dimm array

1.Standered metod

array[0][0]=66;
array[0][1]=54;
array[0][2]=36;
array[0][3]=59;
array[0][4]=50;
array[1][0]=55;
array[1][1]=59;
array1][2]=54;
array[1][3]=59;
array[1][4]=96;
Examples 1
class arr{
public static void main(String args[]){
 int[][] x;
 x=new int[6][2];
 x[0][0]=34;
 x[0][1]=56;
 x[1][0]=77;
 x[1][1]=73;
 x[2][0]=14;
 x[2][1]=15;
 x[3][0]=16;
 x[3][1]=07;
 x[4][0]=18;
 x[4][1]=19;
 x[5][0]=25;
 x[5][1]=26;


System.out.println(x[0][0]);
System.out.println(x[0][1]);
System.out.println(x[1][0]);
System.out.println(x[1][1]);
System.out.println(x[2][0]);
System.out.println(x[2][1]);


}
 }

 

When the above code is executed it produces the following result
Example
Examples 2

class Arraylast{

public static void main(String args[]){
int[][] x;
x=new int[6][2];
x[0][0]=10;
x[0][1]=11;
x[1][0]=12;
x[1][1]=13;
x[2][0]=14;
x[2][1]=15;
x[3][0]=16;
x[3][1]=17;
x[4][0]=18;
x[4][1]=19;
x[5][0]=20;
x[5][1]=21;

for(int i=0;i<=5;i++)
{
for (int j=0;j<=1;j++)
{

System.out.println(x[i][j]);
}
}
}

When the above code is executed it produces the following result

Example

Initialize during the declaration

int[][] arr={
{45,76,98,65},
{36,78,76,55},
{56,66,56,24},
};
Example
class ArrayTwoDim{
public static void main(String args[]){
int[][] arr={
{45,76,98,65},
{36,78,76,55},
{56,66,48,24},
};

//first row of elements
System.out.println(arr[0][0]);
System.out.println(arr[0][1]);
System.out.println(arr[0][2]);
System.out.println(arr[0][3]);
System.out.println();
//second row of elements
System.out.println(arr[1][0]);
System.out.println(arr[1][1]);
System.out.println(arr[1][2]);
System.out.println(arr[1][3]);
System.out.println();
//third row of elements
System.out.println(arr[2][0]);
System.out.println(arr[2][1]);
System.out.println(arr[2][2]);
System.out.println(arr[2][3]);

}
}

When the above code executed it produces the following output

Example

Initialize and display elements to the array using loops

Using the for loop
import java.util.Scanner;
class ArrayWithForTwoDim{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the row length for an array: ");
int row=scan.nextInt();
System.out.print("Enter the coloum length for an array: ");
int column=scan.nextInt();
int arr[][]=new int[row][column];//array declaration
System.out.print("\nEnter "+row*column+" Element to two dim array: \n");
for(int i=0; i<row; i++){
   for(int j=0; j<column; j++){
      arr[i][j]=scan.nextInt();
}
}
System.out.print("\ndisplay Element in an array: \n");
for(int i=0; i<row; i++){
   for(int j=0; j<column; j++){
      System.out.print("\ndisplay Element Row["+i+"]:column["+j+"]:"+arr[i][j] );
}
}
}
}

When the above code executed it produces the following output

Example
Using the while loop
import java.util.Scanner;
class ArrayWithWhileTwoDim{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the row length for an array: ");
int row=scan.nextInt();
System.out.print("Enter the coloum length for an array: ");
int column=scan.nextInt();
int arr[][]=new int[row][column];//array declaration
System.out.print("\nEnter "+row*column+" Element to two dim array: \n");
int i=0;
while( i<row){
  int j=0;
   while( j<column){
      arr[i][j]=scan.nextInt();
    j++;
}
i++;
}
System.out.print("\ndisplay Element in an array: \n");
i=0;
while( i<row){
  int j=0;
   while( j<column){
      System.out.print("\ndisplay Element Row["+i+"]:column["+j+"]:"+arr[i][j] );
    j++;
}
i++;
}
}
}

When the above code executed it produces the following output

Example
Using the do-while loop
import java.util.Scanner;
class ArrayWithDoWhileTwoDim{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the row length for an array: ");
int row=scan.nextInt();
System.out.print("Enter the coloum length for an array: ");
int column=scan.nextInt();
int arr[][]=new int[row][column];//array declaration
System.out.print("\nEnter "+row*column+" Element to two dim array: \n");
int i=0;
do{
  int j=0;
   do{
      arr[i][j]=scan.nextInt();
    j++;
}while( j<column);
i++;
}while( i<row);
System.out.print("\ndisplay Element in an array: \n\n");
i=0;
do{
  int j=0;
   System.out.print("\nrow "+(i+1)+" elements");
   
   do{
    
      System.out.print("\ndisplay Element Row["+i+"]:column["+j+"]:"+arr[i][j] );
    
    j++;
}while( j<column);
i++;
System.out.print("\n");
}while( i<row);
}
}

When the above code executed it produces the following output

 

Display output using for each loop in two dimension array

class arrayforeach{
public static void main (String args[]){
int[]array[]=new int[3][3];
array[0][0]=87;
array[0][1]=67;
array[0][2]=07;
array[1][0]=83;
array[1][1]=88;
array[1][2]=81;
array[2][0]=67;
array[2][1]=74;
array[2][2]=828;
for(int i[]:array){
for(int j:i){
System.out.println(j);
}
}
}
}
Example
When the above code executed it produces the following output
Output
87
67
7
83
88
81
67
74
828

One dim Array in  Java       One dim Array in  C++       One dim Array in  C

Two dim Array in  Java       Two dim Array in  C++      Two dim Array in  C

Three dim Array in  Java    Three dim Array in  C++   Three dim Array in  C

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.