Sat. Apr 27th, 2024

The multi dimension arrays in java will be represented in the form of array of arrays 

Syntax for declaring a two dimension array: 

datatype arrayName[][]; 

Rule 1: At the time of array declaration we can specify the pair of [][] either before the array name or after the array name. 

int arr1[][]; 

int[][] arr2; 

int [][]arr3; 

int[] []arr4; 

int []arr5[]; 

int[] arr6[]; 

Rule 2: At the time of array declaration we should not specify size of the array 

Syntax for creating 2 dimensional array: 

datatype arrayName[][] = new datatype[size1][size2); 

Or 

datatype arrayName[][]; 

arrayName = new datatype[size1][size2); 

Example: 

int arr[][] = new int[3][4]; 

Or 

int arr[][]; 

arr = new int[3][4]; 

Rule: In a multi dimension array specifying the first dimension of array is mandatory and the remaining are optional. If we are specifying the remaining dimensions then they must be specified in the order from left to right 

Jagged array: In multi dimension array of the arrays have unequal size then they are called Jagged array 

Example of Jagged array 

Creation of the above jagged array 

nt[][] arr = new int[3][]; 

arr[0] = new int[3]; 

arr[1] = new int[2]; 

arr[2] = new int[4] 

class ArrayDemo { 

   public static void main(String[] args) { 

int arr[][] = {{1.2.3}, {4,5,6}, {7,8,9}}; 

for(int i=0) i<arr.length; i++) { 

for(int j=0; j< arr[i].length; j++) { 

System.out.print(arr[i][j]+"") 

 } 

System.out.println(); 

} 

for(int[] x arr) { 

 for(int y: x) { 

 System.out.print (y+" "); 

} 

System.out.println(); 

    } 

  } 

} 

Example

class Student { 

public static void main(String[] args) { 

int[] marks= {91,95,100,89,79,99}; 

int total = 0; 

boolean result = true; 

System.out.println("student report card"); 

for(int i=0; i<marks.length; i++) { 

System.out.println("subject" + (i+1))+"marks: "+marks[i]) 

Total= total +marks[i]; 

if(marks[i] <35) 

result = false; 

} 

System.out.printin("total marks"+total); 

int avg = total/marks.length; 

System out println("average: "+avg); 

if(result) { 

if(avg >= 75) { 

System.out.println("Grade Distinction"); 

} 

else if(avg >= 60) { 

System out println("Grade First Class"); 

} 

else if(avg >= 50) { 

System.out.println("Grade Second Class"); 

} 

else ( System.out.println("Grade Third Class") 

 }  

} 

else {  

System.out.println("Welcome Again"); 

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *