Join Our 4-Week Free Gen AI Course with select Programs.

Request a callback

or Chat with us on

Multidimensional Arrays in Java – A Beginner’s Guide

Basics of SQL
Basics of SQL
icon
12 Hrs. duration
icon
12 Modules
icon
2600+ Learners
logo
Start Learning

An array is a homogeneous data structure that stores data of the same type in contiguous memory locations. Multidimensional arrays in Java allow for creating complex data structures to represent data in a grid-like format. This guide will help you understand multidimensional arrays, how to declare and initialize them, and how to use them in your Java programs.

Introduction to Multidimensional Array in Java

Multidimensional arrays are arrays of arrays. They store data in a grid or matrix-like format, particularly useful for mathematical computations, games, and any situation where data naturally fits into a table-like structure. The most common type of multidimensional array is the two-dimensional array.

 

Key Concepts 

 

  1. Definition: A multidimensional array is essentially an array where each element is an array. In Java, that is represented as an array of arrays.
  2. Syntax: Defining a multidimensional array involves multiple sets of square brackets. For example, a two-dimensional array of integers can be declared as int[][] arrayName.
  3. Initialization: Multidimensional arrays can be initialized in various ways, including specifying the size of each dimension or providing a list of values for each sub-array.
  4. Accessing Elements: The elements in a multidimensional array are accessed using multiple indices, one for each dimension. For example, arrayName[0][1] accesses the element in a two-dimensional array’s first row and second column.

 

The following program demonstrates the declaration of a two-dimensional array in Java.

 

Program

class Main{ public static void main(String args[]){ int [][] matrix = { {1,2,3}, {4,5,6}, {7,8,9} } ; for(int i =0;i< matrix.length; i++){ for(int j = 0;j<matrix.length;j++){ System.out.print(matrix[i][j]+" "); } } System.out.println(); } }

Output

 

1 2 3 4 5 6 7 8 9

Syntax of Multidimensional Array in Java

The general syntax to declare a multidimensional array. DataType[1st Dimension][2nd Dimension]....[Nth Dimension] arrayName; General syntax to initialize the array. arrayName = new DataTye[length 1][length 2]....[length N]; Or arrayName = new DataType[length 1][length 2]....[length N];

Types of Multidimensional Array in Java

A multidimensional array can be a 2D array, a 3D array, a 4D array, where D stands for dimension. However, the higher the dimension, the more difficult it is to access and store elements.

 

We will discuss 2D arrays in Java and 3D arrays in Java in detail.

 

 

  1. Two-dimensional array or 2D array: Two-dimensional arrays are Java’s most common multidimensional array type. They are mostly used in Java and can be visualized as a table or a matrix with rows and columns.

 

The following program demonstrates the two-dimensional array in Java language.

 

Program

class Main { public static void main(String args[]) { int[][] twoDArray = new int[3][4]; int[][] predefinedArray = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; for(int i = 0;i

 

Output

 

1 2 3 5 6 7 9 10 11

 

  1. Three-dimensional Arrays: Three-dimensional arrays add another layer of complexity and can be visualized as a cube of data.

 

The following program demonstrates a 3D array.

 

Declaration and Initialization

class Main { public static void main(String args[]) { // Declaration int[][][] threeDArray; threeDArray = new int[3][4][5]; int[][][] anotherThreeDArray = new int[3][4][5]; // Using array literals int[][][] predefinedThreeDArray = { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } }; } }
  1. Higher Dimensional Arrays: The less common Java also supports arrays with more than three dimensions. These arrays can become complex to manage and visualize.

 

The following program demonstrates the Declaration and Initialization:

 

Program

class Main { public static void main(String args[]) { int[][][][] fourDArray; fourDArray = new int[3][4][5][6]; int[][][][] anotherFourDArray = new int[3][4][5][6]; // Using array literals for simplicity (though rare) int[][][][] predefinedFourDArray = { { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }, { { { 9, 10 }, { 11, 12 } }, { { 13, 14 }, { 15, 16 } } } }; } }
DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

Operations on Multidimensional Arrays

In Java, many operations can be performed on multidimensional arrays. We can add, subtract, and multiply them. In this section, we will multiply two-dimensional arrays similar to matrix multiplication.

 

The following program demonstrates the multidimensional Arrays in Java.

 

Program

import java.util.*; public class Main { public static void main(String[] args){ int[][] array1 = { {1,2,1},{3,4,1}}; int[][] array2 = { {2,3},{4,1},{8,3}}; int row1 = array1.length, row2 = array2.length, col1 = array1[0].length, col2 = array2[0].length; if (row2 != col1) { System.out.println("number of rows of array1 is not equal to the number of columns of array2"); } else{ int[][] arrayAns = new int[row1][col2]; for (int i = 0; i < row1; i++) { for (int j = 0; j < col2; j++) { for (int k = 0; k < row2; k++) arrayAns[i][j] += array1[i][k] * array2[k][j]; } } for(int[] temp : arrayAns){ for(int val : temp){ System.out.print(val+" "); } System.out.println(); } } } }

Output

18 8 30 16

 

Application of Multi-Dimensional Array

Multidimensional arrays in Java are powerful tools for handling complex data structures. They allow you to store data in a grid-like format, which can be useful in various scenarios.

 

  1. Matrices and Mathematical Computations:  Multidimensional stores matrix elements and performs addition, subtraction, and multiplication operations.

 

  1. Image Processing: Images are represented as 2D arrays, where each element is a pixel. This method applies filters, resizes, and rotates images.

 

  1. Storing Data in Tables: A multidimensional data structure stores tabular data, such as student marks in different subjects. It easily calculates averages and other statistics.

 

  1. Simulating Real-World Systems: This simulates traffic grids, weather patterns, or cellular automata. They represent and manipulate complex data structures in a grid format.

 

Conclusion

 

In this article, we discussed multidimensional arrays in Java which are powerful and versatile tools for managing and organizing complex data structures. They allow for creating arrays with multiple dimensions, such as 2D arrays (tables) and 3D arrays (cubes), providing a structured way to store data. At the same time, they offer significant advantages regarding data organization and manipulation. They also come with complexities related to memory management and accessibility.

FAQs
A multidimensional array in Java is an array of arrays. It’s a way to represent data in a tabular form, such as matrices or tables, where each element can be accessed using multiple indices
We can declare a multidimensional array in Java using the following syntax.   The following program demonstrates the multidimensional array in Java.
int[][] arrayName;
We can initialize a multidimensional array at the time of declaration or separately.
class Main{

public static void main(String args[]){

int rows = 3;

int columns = 5 ;

int[][] arrayName = new int[rows][columns];


}

}
We can access elements in a multidimensional array using their indices for a two-dimensional array.
int element = arrayName[rowIndex][columnIndex];
Yes, Java allows the creation of jagged arrays, where each row can have a different number of columns.
int[][] jaggedArray = new int[3][];

jaggedArray[0] = new int[2];

jaggedArray[1] = new int[4];

jaggedArray[2] = new int[3];

Deploying Applications Over the Cloud Using Jenkins

Prashant Kumar Dey

Prashant Kumar Dey

Associate Program Director - Hero Vired

Ex BMW | Google

24 October, 7:00 PM (IST)

Limited Seats Left

Book a Free Live Class

left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved