Java

3D Arrays in Java

An array can be one-dimensional or multidimensional. The array that contains one dimension and the values of the array that are assigned, accessed, and modified by a single index is called a one-dimensional array. The array that contains two or more dimensions and the values of the array that are accessed or modified by multiple indexes is called a multidimensional array. A 3D array is a multi-dimensional array that contains three indexes to access the array values. The methods of using the 3D arrays in Java are shown in this tutorial.

Example 1: Initialize the Elements of 3D Arrays

Create a Java file with the following code where a 3D array of strings is declared with three indexes. The index values of the array are 3, 2, and 1. So, the total elements of the array is 3x2x1 = 6. Next, the six-string values are assigned to the array. If the indexes and the values are assigned properly for the array, a success message is printed in the output.

public class multiArray1 {

    public static void main(String[] args) {
       
        //Declare a 3D array
        String [] [] [] courses = new String [3] [2] [1];      
        //Initialize the array
        courses[0][0][0] = "CSE-101";
        courses[0][1][0] = "CSE-103";
        courses[1][0][0] = "CSE-205";
        courses[1][1][0] = "CSE-206";
        courses[2][0][0] = "CSE-308";
        courses[2][1][0] = "CSE-307";
       
        System.out.println("A 3D array has been initialized.");
    }


}

Output:

The following output appears after executing the code. The “A 3D array has been initialized” success message is printed in the output:

Example 2: Declaring the Array with Values

Create a Java file with the following code where a 3D array of double numbers is defined with the values. The index values of the array are not mentioned in the declaration. According to the array values, the indexes are 4, 3, and 3. So, the total number of elements of the array is 4x3x3 = 36. The length of each index can be found using the length property of the array. The first length is counted using the arrayName.length. The second length is counted using the arrayName[0].length. The third length is counted using the arrayName[0][0].length.  Next, these length values are multiplied to find out the total elements of the array.

public class multiArray2 {

    public static void main(String[] args) {
       
        //Declare a 3D array
        double cgpa [] [] [] = { {
             {3.67 , 3.32, 3.78}, {3.23 , 3.36, 3.56} , {3.90 , 3.34, 3.12}
                 },
                 {
                     {3.67 , 3.32, 3.78}, {3.67 , 3.32, 3.78} , {3.67 , 3.32, 3.78}
                     },
                 {
                         {3.55 , 3.78, 3.43}, {3.67 , 3.39, 3.18} , {3.56 , 3.12, 3.51}
                     },
                 {
                         {3.63 , 3.87, 3.29}, {3.61 , 3.37, 3.76} , {3.67 , 3.45, 3.40}
                     }
         };
        //Count the first index of the array
        int len1 = cgpa.length;
        //Count the second index of the array
        int len2 = cgpa[0].length;
        //Count the third index of the array
        int len3 = cgpa[0][0].length;
        //Calculate the total elements of the array
        int arrlen = len1*len2*len3;
       
        System.out.println("A 3D array has been defined.");
        System.out.println("Total elements of the array is " + arrlen);
    }

}

Output:

The following output appears after executing the code. The “A 3D array has been defined” success message and the total elements of 36 are printed in the output:

Example 3: Access the Elements of 3D Arrays

In the previous two examples, two types of 3D array declaration in Java are shown. Create a Java file with the following code where the method of accessing the values of the 3D array is shown. Here, a 3D array of 3, 2, and 1 indexes is initialized. Next, the value of each index is stored into three variables using the length property like the previous example. These values are used in three “for” loops to access the value of each index of the array and are printed in the output.

public class multiArray3 {

    public static void main(String[] args) {
       
        //Declare a 3D array
        String [] [] [] courses = new String [3] [2] [1];      
        //Initialize the array
        courses[0][0][0] = "CSE-101";
        courses[0][1][0] = "CSE-103";
        courses[1][0][0] = "CSE-205";
        courses[1][1][0] = "CSE-206";
        courses[2][0][0] = "CSE-308";
        courses[2][1][0] = "CSE-307";
       
        //Count the length of each index
        int l1 = courses.length;
        int l2 = courses[0].length;
        int l3 = courses[0][0].length;
       
        //Print the array values
        System.out.println("Array values are: ");


        for(int i = 0; i < l1; i++)

        {

            for(int j = 0; j < l2; j++)

            {

                for(int k = 0; k < l3; k++)

                {
                    System.out.println(courses[i][j][k]);
                }
            }
           
        }
       
    }


}

Output:

The following output appears after executing the code. Six values of the array are printed in the output:

Example 4: Remove the Element of 3D Arrays

There is no direct built-in function in Java to remove the elements from the front or the back of the 3D array like in the one-dimensional array. The value of the particular index of the 3D array can be made empty by assigning the non-empty string value in that particular index value. Create a Java file with the following code that resets the particular index of the 3D array by the empty string. Next, the array values are printed by the “for” loops. You can update the value of any index of a 3D array in the same way.

public class multiArray4 {

    public static void main(String[] args) {
       
        //Declare a 3D array
        String [] [] [] courses = new String [3] [2] [1];      
        //Initialize the array
        courses[0][0][0] = "CSE-101";
        courses[0][1][0] = "CSE-103";
        courses[1][0][0] = "CSE-205";
        courses[1][1][0] = "CSE-206";
        courses[2][0][0] = "CSE-308";
        courses[2][1][0] = "CSE-307";
       
//Unset the data of the particular index
        courses[1][0][0] = "";
        //Count the length of each index
        int l1 = courses.length;
        int l2 = courses[0].length;
        int l3 = courses[0][0].length;
       
        //Print the array values
        System.out.println("Array values are: ");

        for(int i = 0; i < l1; i++)

        {

            for(int j = 0; j < l2; j++)

            {

                for(int k = 0; k < l3; k++)

                {

                    System.out.println(courses[i][j][k]);
                }
            }
           
        }
       
    }


}

Output:

The following output appears after executing the code:

Conclusion

The methods of defining, assigning, accessing, and updating the 3D arrays in Java are shown in this tutorial using multiple Java classes that will help the Java users to understand the concept of the 3D array.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.