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.

39,914 questions

51,847 answers

573 users

How to create and set values to a 3d array in Java

1 Answer

0 votes
public class ThreeDArray {
    
    public static void initializeArray(int[][][] array, int x, int y, int z) {
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                for (int k = 0; k < z; k++) {
                    array[i][j][k] = i + j + k; // Initialization
                }
            }
        }
    }

    public static void printArray(int[][][] array, int x, int y, int z) {
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                for (int k = 0; k < z; k++) {
                    System.out.print(array[i][j][k] + " ");
                }
                System.out.println();
            }
        }
    }

    public static void main(String[] args) {
        int x = 2, y = 3, z = 4;
        int[][][] array = new int[x][y][z]; // Create a 3D array

        initializeArray(array, x, y, z);
        printArray(array, x, y, z);
    }
}



/*
run:

0 1 2 3 
1 2 3 4 
2 3 4 5 
1 2 3 4 
2 3 4 5 
3 4 5 6 

*/

 



answered Apr 21, 2025 by avibootz
...