Three Dimensional Array in Java | 3D Array, Example

An array with three indexes (subscripts) is called three dimensional array in Java. In other words, a three-dimensional array is a collection of one or more two-dimensional arrays, all of which share a common name.

It is distinguishable by the values of the first subscript of three dimensional array. Thus, a three-dimensional array is an array of two-dimensional arrays.

Similarly, a four dimensional array is an array of three dimensional arrays. In general, an n-dimensional array is an array of (n – 1) dimensional arrays.

Use of 3D Array in Java


Java three dimensional array is useful when we want to handle a group of elements belonging to another group. For example, suppose a college has three departments: Electronics, Computer Science, and Information Technology.

We can represent exam scores obtained by three students of each department in 3 different subjects like this:

Electronics department:

student1 scores: 75, 87, 69
student2 scores: 90, 87, 85
student3 scores: 56, 67, 76

Computer Science department:

student1 scores: 78, 67, 75
student2 scores: 87, 98, 76
student3 scores: 67, 56, 65

Information Technology department:

student1 scores: 72, 63, 72
student2 scores: 82, 91, 71
student3 scores: 64, 56, 66

To store all these exam scores, department-wise, we will need to use java three-dimensional array. The syntax below declares a three-dimensional array of variable scores, creates an array, and assigns its reference to scores:

int[ ][ ][ ] scores = new int[3][3][3];

In the above syntax, three pairs of square braces represent a 3D array. There are three rows representing three departments. In each department, there are again three groups representing the scores of three students.
[adinserter block=”5″]
We can also use the shorthand notation to create and initialize the array as follows:

int[ ][ ][ ] scores = {
                                   {{75, 87, 69}, {90, 87, 85},{56, 67, 76}},
                                   {{78, 67, 75}, {87, 98, 76}, {67, 56, 66}},
                                   {{72, 63, 72}, {82, 91, 71}, {64, 56, 66}}
                                   };

scores[0][1][0] refers to the subject score obtained by student2 in the electronics department which is 90.

Similarly, scores[0][1][2] refers to the subject score obtained by student1 in the electronics department which is 85.

scores[2][2][0] refers to the subject score obtained by the student3 in the information technology department which is 64.

3D Array declaration in Java


The general syntax to declare 3D array in java is as follows:

data-type[ ][ ][ ] variableName;
variableName = new data-type[size1][size2][size3];
Or,
data-type[ ][ ][ ] variableName = new data-type[size1][size2][size3];

Here, data-type refers to any data type supported by Java. variableName represents array name.

size1 represents the number of tables being grouped together. size2 represents the number of rows of each table. size3 represents the number of columns of each table.

Each element in a three-dimensional array is accessed or identified by the array name followed by a pair of square braces enclosing table-number, followed by a pair of square braces enclosing row-number, followed by a pair of square braces enclosing column-number.

The table-number ranges from 0 to (size1 – 1). For example, when table-number is 0, first table is selected. Similarly, when the table-number is 1, second table is selected, and so on.

The row-number ranges from 0 to (size2 – 1). For example, when the row-number is 0, first row is selected. When row-number is 1, second row is chosen, and so on.
[adinserter block=”2″]
The column-number ranges from 0 to (size3 – 1). When the column-number is 0, first column is selected, column-number is 2, second column is selected, and so on.

Let’s understand it with the help of a simple example.

int[ ][ ][ ] x = new int[2][3][4];

In this example, x is declared to be a 3D array in which there are two table values, each table having three rows and four columns.

The range of table-number is from 0 to 1. The range of row is from 0 to 2 and the column range from 0 to 3.

For example:

x[0][0][0] refers to the data in the first table, first row, and first column.
x[1][0][0] refers to the data in the second table, first row, and first column.
x[1][2][3] refers to the data in the second table, thrid row, and fourth column.


Key points:

a) A multidimensional array in java is actually an array in which each element represents another array.

b) The difference between three-dimensional array and two-dimensional array is that 3D array consists of an array of 2D arrays, whereas, 2D array consists of an array of 1D arrays.

c) Three for loops (nested loop) are used to access each element in the tables.

d) First for loop is used to select each table.

e) Second for loop is used to select each row of the selected table.

f) Third for loop is used to access each element (value) in the row and table selected.

Three Dimensional (3D array) Example Programs


Let’s create a program in which we will create a 3D array that consists of department-wise student marks. There are 3 departments such as Electronics, CS, and IT.

In each department, there are 3 students and each student has scored in 3 subjects. We will calculate the total marks obtained and their percentage for each student.

Here, tables represent departments, rows indicate students, and columns indicate subject scores. Look at the following source code to understand better.

Program code:

package arraysProgram;
public class ThreeDArray {
public static void main(String[] args) 
{
 String[ ] department = {"Electronics", "CS", "IT"};
 int dept, st, sc, total = 0;
 double perc = 0;

// Take the scores of students in a 3D array.
    int[ ][ ][ ] scores = {
		                    {{75, 87, 69}, {90, 87, 85},{56, 67, 76}}, 
		                    {{78, 67, 75}, {87, 98, 76}, {67, 56, 66}}, 
		                    {{72, 63, 72}, {82, 91, 71}, {64, 56, 66}}
		                   };
// Display the scores of students from 3D array.
   for(dept = 0; dept < 3; dept++)
   {	
	for(int i = 0; i < 3; i++)
	{
	   System.out.println("Department " +department[i]+ ": "); 
	   for(st = 0; st < 3; st++)
	   {
	      System.out.println("Student" +(st + 1)+ " scores: ");	 
	      for(sc = 0; sc < 3; sc++)
	      {
		 System.out.print(scores[dept][st][sc]+ " ");
		 total += scores[dept][st][sc];
		 perc = (double)total/3;
	      }
	      System.out.println("\nTotal scores: " +total); // Displaying total marks of student.
	      System.out.println("Percentage: " +perc); // Displaying percentage. 
	      total = 0; // reset total to zero.
	   }
	   System.out.println();
	 }
	break;
	}
   }
}
Output:
Department Electronics: 
           Student1 scores: 75 87 69 
           Total scores: 231
           Percentage: 77.0
           
           Student2 scores: 90 87 85 
           Total scores: 262
           Percentage: 87.33333333333333

           Student3 scores: 56 67 76 
           Total scores: 199
           Percentage: 66.33333333333333

Department CS: 
           Student1 scores: 75 87 69 
           Total scores: 231
           Percentage: 77.0
       
           Student2 scores: 90 87 85 
           Total scores: 262
           Percentage: 87.33333333333333
 
           Student3 scores: 56 67 76 
           Total scores: 199
           Percentage: 66.33333333333333

Department IT: 
           Student1 scores: 75 87 69 
           Total scores: 231
           Percentage: 77.0

           Student2 scores: 90 87 85 
           Total scores: 262
           Percentage: 87.33333333333333

           Student3 scores: 56 67 76 
           Total scores: 199
           Percentage: 66.33333333333333

Let’s take another simple example where we will create a 3D array and display them on the console.

Program code:

package arraysProgram;
public class ThreeDArray {
public static void main(String[] args) 
{
    int[ ][ ][ ] x;
         x = new int[3][3][3];

    for(int i = 0; i < 3; i++)
    {
       for(int j = 0; j < 3; j++)	 
           for(int k = 0; k < 3; k++)
	       x[i][j][k] = i + 1;
    }
    for(int i = 0; i < 3; i++)
    {
        System.out.println("Table-" +(i + 1));
        for(int j = 0; j < 3; j++)
        {
	   for(int k = 0; k < 3; k++)
	   System.out.print(x[i][j][k] +" ");	 
	   System.out.println();
        }
        System.out.println();  
    }
  }
}
Output:
        Table-1
          1 1 1 
          1 1 1 
          1 1 1 
        Table-2
          2 2 2 
          2 2 2 
          2 2 2 
        Table-3
          3 3 3 
          3 3 3 
          3 3 3 

In this program, x has been declared to be a 3d array, of dimension (size) 3*3*3. The 3d array x consists of three tables, each contains three rows and three columns.

The outermost for loop is to select each table. The inner for loop has been used to select each row of the table selected by i. The innermost for loop has been used to select elements of the row selected by j. Thus, all the elements of three tables can be accessed.


This tutorial has explained almost all the important points related to three dimensional array (3D array) in Java with the help of important example programs. Hope that you will have understood the basic concepts of creating Java 3D array and practiced all programs. In the next, we will learn how to pass arrays to methods in Java.
Thanks for reading!!!

⇐ Prev Next ⇒