Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,003 questions

51,950 answers

573 users

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 272 views
1 answer 95 views
1 answer 129 views
129 views asked Aug 29, 2020 by avibootz
1 answer 165 views
2 answers 192 views
...