Multidimensional Arrays in java
In Java programming, multidimensional array is a collection of arrays of a group or column array. Multidimensional array in Java programs can store user defined data and information in multiple dimension row column matrix format. Such as, 2D array (table or grid format data), 3D array data, or user defined custom large dimensions containing data and information. Multidimensional arrays in Java are used when the Java programmer has to handle or manage such data and information in the program. Which gets stored and fitted in multi-dimensional structure order according to the programmer. Such as, tabular data, matrices, grids, or multi-layered tabular data, etc.

Two-dimensional array (2D array) in Java.
A two-dimensional array declared in a Java program is a grouped collection of arrays in a user-defined row column format. Where each array element stored in a 2D array is itself an array data type, which represents a row or column of information data in a tabular format stored in the program.
Syntax of 2D Array Declaration in Java.
type[] arrayName;
Along with this, you can also use this format to declare 2D array in Java program.
type arrayName[][];
Initializing a 2D Array in Java.
2D array with a specific user defined value.
int[]] table = {
{9, 10, 07},
{13, 17, 11},
{02, 04, 14}
};
Non-user defined array data type with value indicating the size of the array.
int[]] table = new int[4][4]; //This creates a 4×4 array table matrix
Example of 2D Array in Java Program.
public class Main {
public static void main(String[] args) {
// here we Declaring and initializing a 2D array (3×3 table matrix) data
int[]] table = {
{9, 10, 07},
{13, 17, 11},
{02, 04, 14}
};
// here we Access the elements of the 2D array
System.out.println(“here it displays the Element at row 1, & column 2 – ” + table[1][1]); // Result – 17
//here we Modifying the element in the 2D array
table[1][1] = 70; // here we Change the element at row 1, column 1
// here we Printing the entire 2D array using nested for loops
for (int p = 0; p < table.length; p++) {
for (int q = 0; q < table[p].length; q++) {
System.out.print(table[p][q] + ” “);
}
System.out.println();
}
}
}
Three-Dimensional Array (3D Array) in Java.
In Java programming, a user-defined three-dimensional array is a multi-dimensional group array collection of arrays of arrays. It is displayed as a 3D grid or cube in the storage location. Three-dimensional arrays In a Java program, three arrays are declared with the [][][] square bracket symbol.
Syntax for declaring a 3D array in Java.
type[][][] arrayName;
Example of 3D Array in Java.
public class Main {
public static void main(String[] args) {
// here we Declaring and initializing a 3D array (3x3x3 table matrix) order display
int[][][] cubes = {
{
{1, 2, 4},
{3, 4, 7}
},
{
{5, 6, 9},
{7, 8, 3}
}
};
// here we Access the elements in the 3D array
System.out.println(“display the Element at [1][1][2] – ” + cubes[1][1][2]); //Output-3
//here it Modifying an element in the qubes 3D array
cubes[1][1][1] = 30; // here we Change the element at array location [1] [1] [1]
// now we Printing the entire 3D array element
for (int p = 0; p < cubes.length; p++) {
for (int q = 0; q < cubes[p].length; q++) {
for (int r = 0; r < cubes[p][q].length; r++) {
System.out.print(cubes[p][q][r] + ” “);
}
System.out.println();
}
System.out.println();
}
}
}
Higher Dimensional Arrays in Java.
In Java programming, Java users can declare a higher multi-dimensional array such as a 4D, or 5D array as per the need. As such, such usage in Java is rarely used in real life, as processing, managing and reading multi-dimensional arrays is a complex task. These declarations follow the same pattern as lower-dimensional arrays in Java. Java users can declare a higher dimensional array by using [][][][] square brackets in a Java program.
type[][][][] arrayName;
Important points to keep in mind when declaring a multi-dimensional array in Java.
- Initialization – A 2D array in Java programs is treated as a grouped collection of arrays, where each user-defined array element is a separate array defined in a row.
- Accessing elements – Java users access desired array elements by providing the array’s row and column indices (for 2D arrays) or the proper exact index element location for higher-dimensional arrays.
- Jagged array – A jagged array used in Java programs can have array elements of different array sizes defined or declared for each row or sub-array. Such an array declaration is known as a jagged array.
Java jaggedArray example.
int[][] jaggedArray = {
{9, 10},
{11, 13, 17},
{18, 20, 81, 77}
};
In the jagged array example, the first array row declares two array elements, the second row defines three array elements, and the third row defines four elements.
Advantages of multidimensional arrays in Java.
- Structures – Arrays in Java programs organize data in a grid or tabular format. For example, matrices, tables, grids, etc. are formats.
- Flexibility – multi-dimensional arrays in Java programs represent more complex data structures.
Drawbacks of multidimensional arrays in Java.
- Fixed size – In Java programs, the size of all arrays is fixed after declaration and initialization. Once an array is declared or created, Java users cannot modify the size of that array.
- Complexity – Managing higher-dimensional arrays in Java is a complex task, and can impact the readability of your program source code.
