How to print 3D array in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        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("arr[%d][%d][%d] ", row, col, depth);        
                }
                System.out.println();      
            }      
            System.out.println();    
        }
        
        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:
 
arr[0][0][0] arr[0][1][0] arr[0][2][0] 
arr[1][0][0] arr[1][1][0] arr[1][2][0] 

arr[0][0][1] arr[0][1][1] arr[0][2][1] 
arr[1][0][1] arr[1][1][1] arr[1][2][1] 

arr[0][0][2] arr[0][1][2] arr[0][2][2] 
arr[1][0][2] arr[1][1][2] arr[1][2][2] 

arr[0][0][3] arr[0][1][3] arr[0][2][3] 
arr[1][0][3] arr[1][1][3] arr[1][2][3] 

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 Jan 14, 2022 by avibootz
edited Jan 15, 2022 by avibootz

Related questions

3 answers 248 views
1 answer 67 views
1 answer 118 views
118 views asked Aug 29, 2020 by avibootz
1 answer 147 views
2 answers 181 views
...