Multi Dimensional Array In Java programming Hands on coding

Multidimensional array in Java;


  • It is a collection of data of same data type.
  • It can be used to store group of data simultaneously.
  • It can store data of same data type. Which means an integer array can store only integer value ,character array can store only character value and so on.
  • We can not fetch data from array directly. For that we have to use index point.
  • The indexing of array always start with 0.
  • Index value is always an integer number.
  • Array may be of different data type like int, char, float etc.


Syntax: 


  • Here a is the name of array.
  • int is the data type of array.
  • Size of array is 3x3 which means we can store maximum 9 values in this array.


Initialization of array method 1:

 int a[3][3]={ {40,50,60},{10,20,30},{70,80,90} };   


Initialization of array method 2:

 int a[][]=new int[3][3];   
 a[0][0]=40;   
 a[0][1]=50;   
 a[0][2]=60;   
 a[1][0]=10;  
 a[1][1]=20;   
 a[1][2]=30;   
 a[2][0]=70;   
 a[2][1]=80;   
 a[2][2]=90;   

Printing of array element method 1:

 import java.util.Scanner;   
 class Easy   
 {   
   public static void main(String[] args)   
   {   
    int a[][]={{40,50,60},{10,20,30},{70,80,90}};                                          
    System.out.println("value at a[0][0]="+a[0][0]);   
    System.out.println("value at a[0][1]="+a[0][1]);   
    System.out.println("value at a[0][2]="+a[0][2]);   
    System.out.println("value at a[1][0]="+a[1][0]);   
    System.out.println("value at a[1][1]="+a[1][1]);   
    System.out.println("value at a[1][2]="+a[1][2]);   
    System.out.println("value at a[2][0]="+a[2][0]);   
    System.out.println("value at a[2][1]="+a[2][1]);   
    System.out.println("value at a[2][2]="+a[2][2]);   
   }   
  }   
 /* ### Output ###   
 value at a[0][0]=40   
 value at a[0][1]=50   
 value at a[0][2]=60   
 value at a[1][0]=10   
 value at a[1][1]=20   
 value at a[1][2]=30   
 value at a[2][0]=70   
 value at a[2][1]=80   
 value at a[2][2]=90 */   


Printing of array element method using loop:

 class Easy   
 {   
   public static void main(String[] args)   
   {   
    int a[][]={{40,50,60},{10,20,30},{70,80,90}};   
    for(int i=0;i<=2;i++)   
    {   
     for(int j=0;j<=2;j++)   
      {   
       System.out.print(a[i][j]+" ");   
      }   
     System.out.println("");   
    }   
  }   
 }   
  /* ### Output ###   
  40 50 60 10 20 30 70 80 90 */  



User Input in Array:

 import java.util.Scanner;   
 class Easy   
 {   
   public static void main(String[] args)   
   {   
    Scanner in=new Scanner(System.in);   
    int a[][]=new int[3][3];   
    int i,j;   
    System.out.println("Enter 9 value one by one");   
    for(i=0;i<=2;i++)   
    for(j=0;j<=2;j++)   
    a[i][j]=in.nextInt();  //printing element of array   
    System.out.println("Element is given below");   
    for(i=0;i<=2;i++)   
   {   
    for(j=0;j<=2;j++)   
    {   
      System.out.print(a[i][j]+" ");   
    }   
    System.out.println("");   
   }   
  }   
 }   
 /* ### Output ###   
 Enter 9 value one by one   
 4   
 8   
 2   
 3   
 7   
 9   
 2   
 8   
 1   
 Element is given below   
 4 8 2   
 3 7 9   
 2 8 1 */   


Post a Comment

0 Comments