Table of Contents
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
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
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]);
}
}
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
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
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
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
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
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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.