A 3D array is essentially an array of arrays of arrays: it’s an array or collection of 2D arrays, and a 2D array is an array of 1D array.
This type of array has 3 dimensions – x, y, z.
Creating a 3D array
Data_typearray_name[ ] [ ] [ ] = new array_name[x][n][m]; |
x = dimension (frame)number (2D array)
n = row number
m = column number

Let’s see some examples below that how we can declare a 3D array.
class const3D{ public static void main(String[] args) { int[][][] arr={{{10,20,30},{40,50,60},{70,80,85}}}; // display 3D array for(inti=0;i<1;i++){ for(int j=0;j<3;j++){ for(int k=0;k<3;k++){ System.out.println(arr[i][j][k]); } } } } } |

import java.util.Scanner; public class const3d_1 { public static void main(String[] args) { intarr[][][]=new int[1][3][3]; inti,j,k; Scanner sc=new Scanner(System.in); for(i=0;i<1;i++){ for(j=0;j<3;j++){ for(k=0;k<3;k++){ System.out.println(“insert value index “+i+” “+j+” “+k+” -> “); arr[i][j][k]=sc.nextInt(); } } } } } |

Let’s create a 3D array of strings
import java.util.Scanner; public class const3D_2 { public static void main(String[] args) { String arr[][][]=new String[1][3][3]; inti,j,k; Scanner sc=new Scanner(System.in); // reading values into index for(i=0;i<1;i++){ for(j=0;j<3;j++){ for(k=0;k<3;k++){ System.out.println(“insert value index “+i+” “+j+” “+k+” -> “); arr[i][j][k]=sc.next(); } } } System.out.println(“complete string 3D array is”); // display values of array for(i=0;i<1;i++){ System.out.println(); for(j=0;j<3;j++){ System.out.println(); for(k=0;k<3;k++){ // System.out.println(“complete string 3D array is”); System.out.print(arr[i][j][k]+”\t”); } } } } } |
