Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Two Dimensional Array in Java language

Posted on November 26, 2016January 29, 2020

Table of Contents

  • Two Dimensional Array in Java
    • Declaration of two-dimensional array
    • Creation of two-dimensional array
    • The default value of array
    • initiation of Two dimm array
      • 1.Standered metod
      • Initialize during the declaration
      • Initialize and display elements to the array using loops

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 in Java language
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;
Two Dimensional Array in Java language
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
Two Dimensional Array in Java language
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

Two Dimensional Array in Java language
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
Two Dimensional Array in Java language
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

Two Dimensional Array in Java language
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

Two Dimensional Array in Java language
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

Two Dimensional Array in Java language
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

Two Dimensional Array in Java language
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);
}
}
}
}
Two Dimensional Array in Java language
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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes