Java Multi-Dimensional Arrays
Last Updated :
23 Apr, 2025
Multidimensional arrays are used to store the data in rows and columns, where each row can represent another individual array are multidimensional array. It is also known as array of arrays. The multidimensional array has more than one dimension, where each row is stored in the heap independently. This allows us to make rows of different sizes. Which is more flexible than the one-dimensional tabular arrays.
Example: This Java program shows how to create and use a multidimensional array.
Java
// Java Program to Demonstrate
// Multi Dimensional Array
import java.io.*;
public class Geeks
{
public static void main(String[] args){
// Multidimensional array declaration
int[][] arr;
// Initializing the size of row and column respectively
arr = new int[1][3];
// Initializing the values
arr[0][0] = 3;
arr[0][1] = 5;
arr[0][2] = 7;
// Display the values using index
System.out.println("arr[0][0] = " + arr[0][0]);
System.out.println("arr[0][1] = " + arr[0][1]);
System.out.println("arr[0][2] = " + arr[0][2]);
}
}
Outputarr[0][0] = 3
arr[0][1] = 5
arr[0][2] = 7
Syntax for Multi-Dimensional Array
data_type[1st dimension][2nd dimension][]..[Nth dimension] array_name = new data_type[size1][size2]....[sizeN];
Parameters:
- data_type: Type of data to be stored in the array. For example: int, char, etc.
- dimension: The dimension of the array created. For example: 1D, 2D, etc.
- array_name: Name of the array
- size1, size2, ..., sizeN: Sizes of the dimensions respectively.
Examples:
// Two dimensional array:
int[][] arr2d = new int[3][5];
// Three dimensional array:
int[][][] arr3d = new int[3][5][7];
Size of Multidimensional Arrays: The size of the multidimensional array can be calculated by multiplying the size of all dimensions of array foe example arrd have the dimensions 3*5 = 15
For example: array int[][][] x = new int[3][5][7] can store a total of (3*5*7) = 105 elements.
Two-Dimensional Array (2D-Array)
Two-dimensional array is the simplest form of a multidimensional array. A 2-D array can be seen as an array storing multiple 1-D array for easier understanding.
Syntax (Declare, Initialize and Assigning)
// Declaring and Intializing
data_type[][] array_name = new data_type[x][y];
// Assigning Value
array_name[row_index][column_index] = value;
A 2-D array can be seen as a table with 'x' rows and 'y' columns where the row number ranges from 0 to (x-1) and column number ranges from 0 to (y-1). A 2-D array 'x' with 3 rows and 3 columns is shown below:
Example 1: We can add the values directly to the array while declaring the array.
Java
// Java Program to demonstrate the use of
// Two Dimensional Array
import java.io.*;
class Main {
public static void main(String[] args){
// Array Intialised and Assigned
int[][] arr = { { 1, 2 }, { 3, 4 } };
// Printing the Array
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++)
System.out.print(arr[i][j]+" ");
System.out.println();
}
}
}
Example 2: Updating the values while executing works both ways can be accepted by user or by some variable.
Java
// Java Program to demonstrate the use of
// Two Dimensional Array
public class Geeks
{
public static void main(String[] args) {
// Row and Columns in Array
int n = 2;
int m = 2;
// Array declared and initialized
int[][] arr = new int[n][m];
int it = 1;
// Assigning the values to array
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = it;
it++;
}
}
// Printing the Array
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
}
Accessing Elements of Two-Dimensional Arrays
In Two dimensional array the the row is present by the i and the column is present by the j and we can get the element using arr[i][j] using the nested loop. arr is the name of the variable which reference to the two dimensional array.
Note: In an array of size N, indices range from 0 to N-1. Thus, row index 2 corresponds to the actual row number 3.
Example: Accessing the elements of 2D array using indexes.
Java
// Java Program to demonstrate Accessing
// Elements of Two-Dimensional Arrays
import java.io.*;
class Main {
public static void main(String[] args){
int[][] arr = { { 1, 2 },
{ 3, 4 } };
// Accessing Element at index
// row=1 and column=1
System.out.println("a[1][1] : " + arr[1][1]);
}
}
Follow the Steps mentioned below to create a Two Dimensional Array with User input:
- First import the Scanner class from the util pcakage at top of the program.
- Then create a Scanner class object. Then give a promt to user to enter the size of row and column.
- Then create a nested loop to take input from user to add element in the multi-dimensional array.
- Then print the multi-dimensional array and close the scanner object.
Example: Java program to demonstrate how to create Two Dimensional Array with User input.
Java
// Java Program for Creating two
// Dimensional array with user Inputs
import java.util.Scanner;
public class Geeks
{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
// Taking Number of Rows and Columns from User
System.out.print("Enter number of rows: ");
int row = sc.nextInt();
System.out.print("Enter number of columns: ");
int col = sc.nextInt();
int[][] arr= new int[row][col];
System.out.println("Enter elements of array: ");
// Taking input from user for each element of array
// using nested for loop
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
arr[i][j]= sc.nextInt();
}
}
System.out.println("Elements of array are: ");
// Printing Elements of Arrays
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print(arr[i][j]+ " ");
}
System.out.println();
}
sc.close();
}
}
Output:
Three - Dimensional Array (3D-Array)
3D-Array is a complex form of a multidimensional array. A 3D-array can be seen as an array of 2D array for easier understanding.
A three-dimensional array can be seen as a table of arrays with 'x' rows and 'y' columns where the row number ranges from 0 to (x-1) and column number ranges from 0 to (y-1). A three - dimensional array with 3 array containing 3 rows and 3 columns is shown below:

Example 1: Java program to show how to create and print 3D array.
Java
// Java Program to Demonstrate
// Three Dimensional Array
import java.io.*;
class Geeks
{
public static void main(String[] args){
// Array Created and Initialized
int[][][] arr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
// Defining the x,y,z in Multi
// Dimensional Array
int n = arr.length;
int m = arr[0].length;
int o = arr[0][0].length;
// Printing the Array
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < o; k++) {
System.out.println("arr[" + i + "][" + j + "][" + k + "] = " + arr[i][j][k]);
}
}
}
}
}
Outputarr[0][0][0] = 1
arr[0][0][1] = 2
arr[0][1][0] = 3
arr[0][1][1] = 4
arr[1][0][0] = 5
arr[1][0][1] = 6
arr[1][1][0] = 7
arr[1][1][1] = 8
Example 2: Java program to assigning the values in 3D array using indexes.
Java
// Java Program to Implement
// Three Dimensional Array
import java.io.*;
class Main {
public static void main(String[] args){
int[][][] arr = new int[2][2][2];
// Three Dimensional x,y,z dimension
int n=arr.length;
int m=arr[0].length;
int o=arr[0][0].length;
int it=1;
// Assigning the values to array
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for(int k=0; k < o; k++){
arr[i][j][k] = it;
it++;
}
}
}
// Printing the Array
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++){
for(int k=0; k < o; k++)
System.out.print(arr[i][j][k] + " ");
System.out.println();
}
System.out.println();
}
}
}
Accessing Elements of Three-Dimensional Arrays
Elements in three-dimensional arrays are commonly referred by x[i][j][k] where 'i' is the array number, 'j' is the row number and 'k' is the column number.
Note: In arrays if size of array is N. Its index will be from 0 to N-1. Therefore, for row_index 2, actual row number is 2+1 = 3.
Example: Accessing the elements of 3D array using indexes.
Java
// Java Program to Demonstrate Accessing
// Three Dimensional Array by Index
import java.io.*;
class Geeks
{
public static void main(String[] args){
// Creating an Array
int[][][] arr = { { { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } } };
// Printing array at index 0 , 0 , 0
System.out.println("arr[0][0][0] = " + arr[0][0][0]);
}
}
Inserting a Multi-Dimensional Array During Runtime
Now we can insert a multidimensional array at runtime here we are going to use the Scanner class and then we take the element of the multidimensional array from the user and then we print the count of the even and odd element which user give as an input.
Example: Taking a input from user of mutidimensional array (Runtime) and print the count of even and odd number given by user.
Java
// Java Program Demonstrating use of
// Multi Dimensional Array
import java.io.*;
import java.util.Scanner;
class Geeks {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
// Number of rows
int n = s.nextInt();
// Initialize a 2D array
int[][] arr = new int[n][];
int t = 0;
// Input for each row
for (int i = 0; i < n; i++) {
int m = s.nextInt();
// Assuming all rows have the same column count
t = m;
arr[i] = new int[m];
for (int j = 0; j < m; j++) {
arr[i][j] = s.nextInt();
}
}
int odd = 0, even = 0;
System.out.println("Rows " + n + " with " + t
+ " Columns");
System.out.println("Elements of Array:");
// Print the entire array and count even/odd numbers
for (int i = 0; i < n; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
// Count even and odd numbers
if (arr[i][j] % 2 == 0) {
even++;
}
else {
odd++;
}
}
System.out.println();
}
// Print the aggregated results
System.out.println("Even: " + even
+ ", Odd: " + odd);
s.close();
}
}
Application of Multi-Dimensional Array
- Multidimensional arrays used to arrange the data in more managable form ( Tabular form ) for example arranging the data of a student such as student roll no and marks and The image representation in 3D matrix.
- In dynamic programming questions, multidimensional arrays are used which are used to represent the states of the problem.
- Apart from these, they also have applications in many standard algorithmic problems like: Matrix Multiplication, Adjacency matrix representation in graphs, Grid search problems.
Similar Reads
Basics of Java
Learn Java - A Beginners Guide for 2024If you are new to the world of coding and want to start your coding journey with Java, then this learn Java a beginners guide gives you a complete overview of how to start Java programming. Java is among the most popular and widely used programming languages and platforms. A platform is an environme
10 min read
Introduction to JavaJava is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read
Similarities and Difference between Java and C++Nowadays Java and C++ programming languages are vastly used in competitive coding. Due to some awesome features, these two programming languages are widely used in industries as well as competitive programming. C++ is a widely popular language among coders for its efficiency, high speed, and dynamic
6 min read
Setting up Environment Variables For Java - Complete Guide to Set JAVA_HOMEIn the journey to learning the Java programming language, setting up environment variables for Java is essential because it helps the system locate the Java tools needed to run the Java programs. Now, this guide on how to setting up environment variables for Java is a one-place solution for Mac, Win
6 min read
Java SyntaxJava is an object-oriented programming language that is known for its simplicity, portability, and robustness. The syntax of Java programming language is very closely aligned with C and C++, which makes it easier to understand. Java Syntax refers to a set of rules that define how Java programs are w
6 min read
Java Hello World ProgramJava is one of the most popular and widely used programming languages and platforms. In this article, we will learn how to write a simple Java Program. This article will guide you on how to write, compile, and run your first Java program. With the help of Java, we can develop web and mobile applicat
6 min read
Differences Between JDK, JRE and JVMUnderstanding the difference between JDK, JRE, and JVM plays a very important role in understanding how Java works and how each component contributes to the development and execution of Java applications. The main difference between JDK, JRE, and JVM is:JDK: Java Development Kit is a software develo
3 min read
How JVM Works - JVM ArchitectureJVM (Java Virtual Machine) runs Java applications as a run-time engine. JVM is the one that calls the main method present in a Java code. JVM is a part of JRE (Java Runtime Environment). Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one
7 min read
Java IdentifiersAn identifier in Java is the name given to Variables, Classes, Methods, Packages, Interfaces, etc. These are the unique names used to identify programming elements. Every Java Variable must be identified with a unique name.Example:public class Test{ public static void main(String[] args) { int a = 2
2 min read
Variables & DataTypes in Java
Java VariablesIn Java, variables are containers that store data in memory. Understanding variables plays a very important role as it defines how data is stored, accessed, and manipulated.Key Components of Variables in Java:A variable in Java has three components, which are listed below:Data Type: Defines the kind
9 min read
Scope of Variables in JavaThe scope of variables is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e., scope of a variable can be determined at compile time and independent of the function call stack. In this article, we will learn about
7 min read
Java Data TypesJava is statically typed and also a strongly typed language because each type of data, such as integer, character, hexadecimal, packed decimal etc. is predefined as part of the programming language, and all constants or variables defined for a given program must be declared with the specific data ty
14 min read
Operators in Java
Java OperatorsJava operators are special symbols that perform operations on variables or values. These operators are essential in programming as they allow you to manipulate data efficiently. They can be classified into different categories based on their functionality. In this article, we will explore different
15 min read
Java Arithmetic Operators with ExamplesOperators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
6 min read
Java Assignment Operators with ExamplesOperators constitute the basic building block of any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
7 min read
Java Unary Operator with ExamplesOperators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions be it logical, arithmetic, relational, etc. They are classified based on the functionality they p
8 min read
Java Relational Operators with ExamplesOperators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
10 min read
Java Logical Operators with ExamplesLogical operators are used to perform logical "AND", "OR", and "NOT" operations, i.e., the functions similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consider
8 min read
Java Ternary OperatorOperators constitute the basic building block of any programming language. Java provides many types of operators that can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provi
5 min read
Bitwise Operators in JavaIn Java, Operators are special symbols that perform specific operations on one or more than one operands. They build the foundation for any type of calculation or logic in programming.There are so many operators in Java, among all, bitwise operators are used to perform operations at the bit level. T
6 min read
Packages in Java
Flow Control in Java
Loops in Java
Jump Statements in Java
Arrays in Java
Arrays in JavaArrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Java Multi-Dimensional ArraysMultidimensional arrays are used to store the data in rows and columns, where each row can represent another individual array are multidimensional array. It is also known as array of arrays. The multidimensional array has more than one dimension, where each row is stored in the heap independently. T
10 min read
Jagged Array in JavaIn Java, a Jagged array is an array that holds other arrays. When we work with a jagged array, one thing to keep in mind is that the inner array can be of different lengths. It is like a 2D array, but each row can have a different number of elements.Example:arr [][]= { {10,20}, {30,40,50,60},{70,80,
6 min read
Strings in Java
Java StringsIn Java, a String is the type of object that can store a sequence of characters enclosed by double quotes, and every character is stored in 16 bits, i.e., using UTF 16-bit encoding. A string acts the same as an array of characters. Java provides a robust and flexible API for handling strings, allowi
9 min read
String Class in JavaA string is a sequence of characters. In Java, objects of the String class are immutable, which means they cannot be changed once created. In this article, we are going to learn about the String class in Java.Example of String Class in Java:Java// Java Program to Create a String import java.io.*; cl
7 min read
StringBuffer Class in JavaThe StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters.Features of StringBuffer ClassThe key features of StringBuffer c
11 min read
Java StringBuilder ClassIn Java, the StringBuilder class is a part of the java.lang package that provides a mutable sequence of characters. Unlike String (which is immutable), StringBuilder allows in-place modifications, making it memory-efficient and faster for frequent string operations.Declaration:StringBuilder sb = new
7 min read
OOPS in Java
Java OOP(Object Oriented Programming) ConceptsJava Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Classes and Objects in JavaIn Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh
11 min read
Java MethodsJava Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.Example: Java program to demonstrate how to crea
8 min read
Access Modifiers in JavaIn Java, access modifiers are essential tools that define how the members of a class, like variables, methods, and even the class itself can be accessed from other parts of our program. They are an important part of building secure and modular code when designing large applications. Understanding de
7 min read
Wrapper Classes in JavaA Wrapper class in Java is one whose object wraps or contains primitive data types. When we create an object in a wrapper class, it contains a field, and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object. Let's check on the wr
6 min read
Need of Wrapper Classes in JavaFirstly the question that hits the programmers is when we have primitive data types then why does there arise a need for the concept of wrapper classes in java. It is because of the additional features being there in the Wrapper class over the primitive data types when it comes to usage. These metho
3 min read