Three-dimensional array in Java
-
Last Updated: December 26, 2023
-
By: javahandson
-
Series
In this article, we will learn what is a three-dimensional array in Java or 3D array in Java with proper examples. If you want to learn more about other Java, Java 8, or Spring concepts then please check the above menu options.
In the previous article, we learned about 2D arrays where the records are stored in the form of rows and columns. The 2D array comprises of X-Axis and Y-Axis. If we have to select an element in a 2D array then we should know the row and column number.
Let us see an example below where we have stored the information of IT department employees in a 2D array named employees[][].
IT Department employees Age Salary Designation John 40 1000 Engineer Rob 46 1200 Sr.Engineer Anna 51 2000 Manager
Find Rob’s salary.
Rob’s information is stored in the 2nd row and his salary is stored in the 2nd column. Hence when combined we get to know the element is stored at position employees[1][1] and the result is 1200.
Note* Since the array index starts from Zero that is the reason the second row and the second column are depicted as [1][1].
3D array moves a step forward and it adds one more dimension i.e. depth.
The 3D array comprises of X-axis, Y-axis, and Z-axis. Here the data will be stored in a cubic form. If we have to identify an element in a 3D array then we should know the row, column, and depth.
A 3D array is a combination of several 2D arrays. In the above example, we have created a 2D array of IT department employees. Now if we have to create multiple 2D arrays of employees from other departments then we can do that using a 3D array.
Below we are depicting 2D arrays of different departments
IT Department employees Age Salary Designation John 40 1000 Engineer Rob 46 1200 Sr.Engineer
Finance Department employees Age Salary Designation Mark 37 900 Accountant Susan 46 1000 Sr.Accountant
Admin Department employees Age Salary Designation James 40 1000 Admin Henry 42 1200 Sr.Admin
We can combine the above 2D arrays in a single 3D array. How to do that? We will see that in the below section.
There are 2 ways of creating a 2D array:
1. We can declare a 3D array and store the values of elements directly at the time of declaration.
String employees[][][] = {{{"John", "40", "1000", "Engineer"},{"Rob", "46", "1200", "Sr.Engineer"}}, {{"Mark", "37", "900", "Accountant"},{"Susan", "46", "1000", "Sr.Accountant"}}, {{"James", "40", "1000", "Admin"},{"Henry", "42", "1200", "Sr.Admin"}}};
Find James’s salary from the Admin department.
a. The Admin department is the 3rd row
{{“James”, “40”, “1000”, “Admin”},{“Henry”, “42”, “1200”, “Sr.Admin”}}
b. James information is in the first column of 3rd row {“James”, “40”, “1000”, “Admin”}
c. Salary information is present at the 3rd position in that column i.e. “1000”.
d. Hence we can fetch that value like employees[2][0][2] i.e. 1000.
package com.java.handson.arrays; public class ThreeDArray { public static void main(String[] args) { String employees[][][] = {{{"John", "40", "1000", "Engineer"},{"Rob", "46", "1200", "Sr.Engineer"}}, {{"Mark", "37", "900", "Accountant"},{"Susan", "46", "1000", "Sr.Accountant"}}, {{"James", "40", "1000", "Admin"},{"Henry", "42", "1200", "Sr.Admin"}}}; System.out.println("James salary is : "+employees[2][0][2]); } } Output: James salary is : 1000
2. We can also declare a 3D array and just assign memory to the array using a new operator. We will not assign values to the array at the time of declaration.
String employees[][][] = new String[3][2][4]; 3 rows 2 columns in each row 4 elements depth
Here JVM has just assigned 3*2*4=24 memory locations for storing the employees but there are no elements stored in the array yet. If we have to store elements in the array then we can do that using the indexes like below.
employees[0][0][0] = "John"; employees[0][1][3] = "Sr.Engineer"; employees[1][1][0] = "Susan";
Write a program to create a 3D array accept the elements from within the program and print it.
package com.java.handson.arrays; public class ThreeDArray { public static void main(String[] args) { String employees[][][] = new String[3][2][4]; employees[0][0][0] = "John"; employees[0][0][1] = "40"; employees[0][0][2] = "1000"; employees[0][0][3] = "Engineer"; employees[0][1][0] = "Rob"; employees[0][1][1] = "46"; employees[0][1][2] = "1200"; employees[0][1][3] = "Sr.Engineer"; employees[1][0][0] = "Mark"; employees[1][0][1] = "37"; employees[1][0][2] = "900"; employees[1][0][3] = "Accountant"; employees[1][1][0] = "Susan"; employees[1][1][1] = "46"; employees[1][1][2] = "1000"; employees[1][1][3] = "Sr.Accountant"; employees[2][0][0] = "James"; employees[2][0][1] = "40"; employees[2][0][2] = "1000"; employees[2][0][3] = "Admin"; employees[2][1][0] = "Henry"; employees[2][1][1] = "42"; employees[2][1][2] = "1200"; employees[2][1][3] = "Sr.Admin"; for (int row=0; row<3; row++) { for (int column=0; column<2; column++) { for (int depth=0; depth<4; depth++) { System.out.print(employees[row][column][depth]+" "); } System.out.print(" "); } System.out.println(); } } } Output: John 40 1000 Engineer Rob 46 1200 Sr.Engineer Mark 37 900 Accountant Susan 46 1000 Sr.Accountant James 40 1000 Admin Henry 42 1200 Sr.Admin
So this is all about three-dimensional arrays in Java with proper examples. If you have any questions on this topic please raise them in the comments section. If you liked this article then please share this post with your friends and colleagues so that they can learn Java and its frameworks in more depth.