简体   繁体   English

打印3D阵列

[英]Printing 3D Arrays

I am trying to print a 3D Array. 我正在尝试打印3D阵列。 This is my code: 这是我的代码:

    public static void print(int [][][] array)
{
    for (int i=0; i<array.length; i++ )
    {
        for (int x=0; x<array[i].length;x++)
        {
            System.out.println();
            System.out.print("row "+ x);
            for (int j=0; j<array[i][x].length;j++)
            {
                    System.out.print (array[i][x][j]+ " ");
            }
        }
    }

Is there something am I doing wrong? 我做错什么了吗? Every time I call 每次我打电话

 ThreeDRay.print(d3);

I get this error: 我收到此错误:

Exception in thread "main" java.lang.NullPointerException at ThreeDRay.print(ThreeDRay.java:13) at ThreeDRayRunner.main(ThreeDRayRunner.java:54) 在ThreeDRayRunner.main(ThreeDRayRunner.java:54)的ThreeDRay.print(ThreeDRay.java:13)处的线程“ main”中的java.lang.NullPointerException异常

The rest of the code is this: 其余代码如下:

    public static void main( String args[] )
{
Scanner keyboard = new Scanner(in);

    out.print("How many matrices do you wish to enter? :: ");
    int matCnt = keyboard.nextInt();

    //instantiate a ThreeDRay

    int[][][] d3= new int [matCnt][][];


    for (int x=0; x < d3.length ;++x){
          d3[x] = new int[0][0];

    for(int i = 0; i < matCnt; i++)
    {
        out.print("What is the size of matrix " + i + " : ");
        int size = keyboard.nextInt();

        int[][] mat = new int[size][size];
        out.println();


        for(int r=0; r<mat.length; r++)
        {
            for(int c=0; c<mat[r].length; c++)
            {
                out.print("Enter a value for spot " + r + " - " + c + " :: ");
               mat[r][c]=keyboard.nextInt();
            }
        }



    if (i==0 && i<matCnt)
    {
    for (int l=0; l<=matCnt; l++){
          out.println("\nThreeDRay before setting mat at spot "+l);



    }
    }


       ThreeDRay.print(d3);


       d3[i] = mat;


        out.println("\nThreeDRay after setting mat at spot "+i);

           ThreeDRay.print(d3);

I have to show how I filled the matrices 我必须证明我如何填充矩阵

 //user inputs values for first matrix 

Before adding: 在添加之前:

Array [0] = 数组[0] =

Array [1] = 数组[1] =

Array [2] = 数组[2] =

//add user inputs //添加用户输入

After adding: 添加后:

Array [0] = row 0 = [...] 数组[0] =行0 = [...]

  row 1 = [...] ... depending on user given size of the first matrix Array[1] = Array[2] = 

//ask for inputs for second matrix //询问第二个矩阵的输入

Before Adding 在添加之前

Array [0] = row 0 = [...] 数组[0] =行0 = [...]

  row 1 = [...] ... depending on user given size of the first matrix Array[1] = Array[2] = 

After adding 添加后

Array [0] = row 0 = [...] 数组[0] =行0 = [...]

  row 1 = [...] ... depending on user given size of the first matrix 

Array[1] = row 0 = [...] 数组[1] =行0 = [...]

  row 1 = [...] ... depending on user given size of the first matrix 

Array[2] = 数组[2] =

You need to close the curly brace in this for loop: 您需要在此for循环中关闭花括号:

    for (int x = 0; x < d3.length; ++x) {
        d3[x] = new int[0][0];
    }

Without the closing brace, your program continues into the next for loop after having initialized only one element of d3 while remaining elements are still null, which gives a NullPointerException when you try to print the entire d3 underway. 如果没有大括号,您的程序将仅初始化d3一个元素,而其余元素仍为null之后,将继续进入下一个for循环,当您尝试打印正在执行的整个d3时,将给出NullPointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM