Multi-Dimensional Array in Java | Easy Explanation with Practical Examples

0
Multidimensional Array in Java | Easy Explanation with Practical Examples
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.

📑 Table of Contents

Multidimensional Array in Java Syntax

data_type array_name[row_size][column_size]={Array_Values};
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.

int a[3][3]={{10,20,30},{40,50,60},{70,80,90}};
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

class Jiocoding {
public static void main(String[] args) {
i
nt 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

An Iterating array in java is a common operation that allows you can access each elements in the array. There are several common methods to iterate through an array in java using a for loop.

Two Dimensional Array for Loop Java

class Jiocoding {
public static void main(String[] args) {
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

class Jiocoding {
public static void main(String[] args) {
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

A dynamic 2d array is a powerful technique because user enter own choice value. This is useful when you need to read values from user input perform calculations to determine the array elements. Arrays initialized at runtime can be modified during program execution.

Multidimensional Array using Scanner in Java  

import java.util.Scanner;
class Jiocoding{
public static void main(String[] args) {
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

A 2d array element update or change the value of an array element by assigning a new value to using index number.

How to Change Value in 2D Array Java  

class Jiocoding {
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. 

Post a Comment

0 Comments
Post a Comment (0)
To Top