![]() |
| Multidimensional Array in Java | Easy Explanation with Practical Examples |
Multi Dimensional Array in Java
Java Array: A java multi-dimensional array is a data structure that can hold multiple values arranged in row and columns using square brackets. Array is a collection of data of same data types. It can store data of same data types means an integer array can store integer value and character array can store character value.
We do not create separate variables like s1, s2, s3, you can create one array to hold all s. Array can be used to group of data simultaneously. We cannot fetch data from array value directly therefore we can use index point value. The indexing of array always starts with 0 and index value is always integer value.
Multidimensional Array in Java Syntax
int a[3][3]={{10.40,60},{20,40,60},{67,89,66}}; |
2D Array Declaration and Initialization in Java
Array is the are several ways to initialize arrays. The choice of initialization method depends on the requirements of your java program. Here are some common methods for initializing arrays in java.
- Array_Data_Type: When the create to first of initialize type of data that the array will hold (integer, float, character etc.).
- Array_Name: You can create a array name is your choice but array name is not matching java keywords. If you can choice array name like for, switch java compiler will be showing error.
- Array_Size: Array size is your choice if you want store array value.
a[0][0]=10 a[0][1]=20 a[0][2]=30 a[1][0]=40 a[1][1]=50 a[1][2]=60 a[2][0]=70 a[2][1]=80 a[2][2]=90 |
The values to initialize the 2-d array with the number of elements inside the curly braces must match the size of the array.
We declare and initialize an integer array row and column. Hera a is the name of array and int is data type of array or size of array is 3X3 means we can store maximum 9 values in multi-dimensional array.
Java Multidimensional Array Programs
int a[][]={{10,20,30},{40,50,60},{70,80,90}}; System.out.println("Value of a[0][0]:"+a[0][0]); System.out.println("Value of a[0][1]:"+a[0][1]); System.out.println("Value of a[0][2]:"+a[0][2]); System.out.println("Value of a[1][0]:"+a[1][0]); System.out.println("Value of a[1][1]:"+a[1][1]); System.out.println("Value of a[1][2]:"+a[1][2]); System.out.println("Value of a[2][0]:"+a[2][0]); System.out.println("Value of a[2][1]:"+a[2][1]); System.out.println("Value of a[2][2]:"+a[2][2]); } } ============OUTPUT============ Value of a[0][0]:10 Value of a[0][1]:20 Value of a[0][2]:30 Value of a[1][0]:40 Value of a[1][1]:50 Value of a[1][2]:60 Value of a[2][0]:70 Value of a[2][1]:80 Value of a[2][2]:90 |
Multidimensional Array Loop Java
Two Dimensional Array for Loop Java
int a[][]={{10,20,30},{40,50,60},{70,80,90}}; int i,j; for(i=0;i<3;i++){ for(j=0;j<3;j++){ System.out.println("Array Value of a["+i+"]["+j+"]:"+a[i][j]); } } } } ============OUTPUT============ Value of a[0][0]:10 Value of a[0][1]:20 Value of a[0][2]:30 Value of a[1][0]:40 Value of a[1][1]:50 Value of a[1][2]:60 Value of a[2][0]:70 Value of a[2][1]:80 Value of a[2][2]:90 |
How to Write a Matrix in Java
int a[][]={{10,20,30},{40,50,60},{70,80,90}}; int i,j; for(i=0;i<3;i++){ for(j=0;j<3;j++){ System.out.print(" "+a[i][j]); } System.out.println(); } } } ============OUTPUT============ 10 20 30 40 50 60 70 80 90 |
Java Dynamic Multidimensional Array
Multidimensional Array using Scanner in Java
Scanner s=new Scanner(System.in); int a[][]=new int[3][3]; int i,j,r,c; System.out.print("Enter Row Size:"); r=s.nextInt(); System.out.print("Enter Column Size:"); c=s.nextInt(); for(i=0;i<r;i++){ for(j=0;j<c;j++){ System.out.print("Enter 2D Array Element["+i+"]["+j+"]:"); a[i][j]=s.nextInt(); } } System.out.println("Your 2D Matrix"); for(i=0;i<r;i++){ for(j=0;j<c;j++){ System.out.print(" "+a[i][j]); } System.out.println(); } } } ============OUTPUT============ Enter Row Size:3 Enter Column Size:3 Enter 2D Array Element[0][0]:10 Enter 2D Array Element[0][1]:40 Enter 2D Array Element[0][2]:69 Enter 2D Array Element[1][0]:78 Enter 2D Array Element[1][1]:46 Enter 2D Array Element[1][2]:55 Enter 2D Array Element[2][0]:66 Enter 2D Array Element[2][1]:99 Enter 2D Array Element[2][2]:77 Your 2D Matrix 10 40 69 78 46 55 66 99 77 |
Update Array Element in Java
How to Change Value in 2D Array Java
public static void main(String[] args) { int i,j,a[][] = {{10, 28, 80},{56, 90, 20},{20,60,70}}; System.out.println("Before Update 2D Matrix"); for(i = 0; i < 3; i++){ for(j = 0; j < 3; j++){ System.out.print(" "+a[i][j]); } System.out.println(); } System.out.println("After Update 2D Matrix"); a[1][0] = 88; for(i = 0; i < 3; i++){ for(j = 0; j < 3; j++){ System.out.print(" "+a[i][j]); } System.out.println(); } } } ============OUTPUT============ Before Update 2D Matrix 10 28 80 56 90 20 20 60 70 After Update 2D Matrix 10 28 80 88 90 20 20 60 70 |
Conclusion of Multi-Dimensional Array in Java
Conclusion: This is a all about multi-dimensional array in java language. In this article cover of every concept of related multi-dimensional array. Array program you can understand simple easy method. If your any query or problems array related than you can contact me by email. I can more easy solution provide.

