How to create 3D array in Java

1 Answer

0 votes
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) throws Exception {
        int[][][] arr = new int[2][3][4];
         
        arr[0][0][0] = 11;
        arr[1][1][1] = 32; 
        arr[0][2][2] = 99;
        arr[0][1][3] = 44;
        arr[1][2][3] = 55;
 
        for (int depth = 0; depth < 4; depth++) {     
            for (int row = 0; row < 2; row++) {
                for (int col = 0; col < 3; col++) {         
                    System.out.printf("%d ", arr[row][col][depth]);        
                }
                System.out.println();      
            }      
            System.out.println();    
        }
    }
}
 
 
 
/*
run:
 
11 0 0 
0 0 0 

0 0 0 
0 32 0 

0 0 99 
0 0 0 

0 44 0 
0 0 55 

*/

 



answered Aug 29, 2020 by avibootz
edited Jan 14, 2022 by avibootz

Related questions

1 answer 67 views
1 answer 125 views
125 views asked Jan 14, 2022 by avibootz
1 answer 148 views
3 answers 249 views
2 answers 182 views
...