Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Arrays in Java

3-Dimensional (3D) Array in Java

A 3D array is an extension of a 2D array, adding another dimension. It can be used to represent students in different schools and classes.

Syntax:

dataType[][][] arrayName = new dataType[depth][rows][columns];

or

dataType[][][] arrayName = {
    {
        {value1, value2},
        {value3, value4}
    },
    {
        {value5, value6},
        {value7, value8}
    }
};

Example:

Students in multiple schools and classes.

class MasterBackend {
    public static void main(String[] args) {
        // 3D Array: Students in multiple schools and classes
        String[][][] schoolStudents = {
            {{"Ayush", "Rahul"}, {"Neha", "Priya"}},  // School 1
            {{"Ravi", "Anita"}, {"Vikas", "Sita"}}    // School 2
        };

        System.out.println("Students in multiple schools and classes:");
        for (int i = 0; i < schoolStudents.length; i++) {
            System.out.println("School " + (i + 1) + ":");
            for (int j = 0; j < schoolStudents[i].length; j++) {
                System.out.print("  Class " + (j + 1) + ": ");
                for (int k = 0; k < schoolStudents[i][j].length; k++) {
                    System.out.print(schoolStudents[i][j][k] + " ");
                }
                System.out.println();
            }
        }
    }
}

Output:

Students in multiple schools and classes:
School 1:
  Class 1: Ayush Rahul
  Class 2: Neha Priya
School 2:
  Class 1: Ravi Anita
  Class 2: Vikas Sita