How to define and use 3D array (cube) of numbers in Java

2 Answers

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            int[][][] arr3d = new int[3][3][3];
            arr3d[0][0][0] = 1;
            arr3d[1][1][1] = 2; // Middle of the cube
            arr3d[2][2][2] = 3;

            System.out.println(arr3d[0][0][0]);
            System.out.println(arr3d[1][1][1]);
            System.out.println(arr3d[2][2][2]);
            
            
            

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:

1
2
3
    
 */

 



answered Nov 29, 2016 by avibootz
0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            int[][][] arr3d = {{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}};
            
            System.out.println(arr3d[0][0][0]);
            System.out.println(arr3d[0][0][1]);
            System.out.println(arr3d[0][1][1]);
            System.out.println(arr3d[0][2][0]);
            System.out.println(arr3d[0][2][1]);

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:

1
2
5
7
8
    
 */

 



answered Nov 29, 2016 by avibootz

Related questions

4 answers 273 views
1 answer 67 views
3 answers 249 views
2 answers 110 views
110 views asked Jul 20, 2020 by avibootz
4 answers 177 views
177 views asked Jul 20, 2020 by avibootz
1 answer 119 views
1 answer 125 views
125 views asked Jan 14, 2022 by avibootz
...