简体   繁体   English

打印3D阵列

[英]Printing 3D Array

I am trying to print arrays before and after they are filled with user given inputs. 我正在尝试在数组中填充用户给定的输入之前和之后。 Therefore, this would be the output if the given size is 3: 因此,如果给定大小为3,则这将是输出:

//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] = 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] = 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] = 

I wrote the following code to achieve this (In a class called ThreeDRayRunner): 我编写了以下代码来实现此目的(在名为ThreeDRayRunner的类中):

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]+ " ");
            }
        }
    }

However,I get the following error: 但是,我得到以下错误:

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

Also, in the runner I ask the user to input the integer that will be stored in the 3D Array. 另外,在运行程序中,我要求用户输入将存储在3D数组中的整数。 To do this I ask for the size and print the 3D Array when is empty and after the given numbers are added. 为此,我要求输入大小并在添加空数字后将其打印为3D阵列。 This is my code for that: 这是我的代码:

Scanner keyboard = new Scanner(in);

    out.print("How many matrices are you going to enter? ");
    int s = keyboard.nextInt();


    int[][][] d3= new int [s][][];

    for(int i = 0; i < S; i++)
    {
        out.print("What is the size of the 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();
            }
        }

       out.println("The array before setting mat at spot "+i);


       ThreeDRay.print(d3);

       d3[i] = mat;


        out.println("The array after setting mat at spot "+i);

       ThreeDRay.print(d3);

You are not instantiating the 2nd and 3rd dimensions of d3 Did you mean to do this line before your first print? 您没有实例化d3的第2维和第3维。您是要在第一次打印之前执行此行吗?

d3[i] = mat;

So it would look like this: 所以它看起来像这样:

d3[i] = mat;

out.println("The array before setting mat at spot "+i);
ThreeDRay.print(d3);

Follow up: 跟进:

It looks like you actually didn't want to set d3[i] before your first print. 看来您实际上不想在第一次打印前设置d3 [i]。 In that case make sure you 3 dimensions for d3 are all non-null since your print is going to print all of them and not only the one that you set to mat. 在这种情况下,请确保d3的3个尺寸都不为空,因为您的打印将打印所有尺寸,而不仅是要设置为填充的尺寸。 Initialize your 2nd and 3rd dimensions (to 0) before you start. 开始之前,请初始化您的第二维和第三维(至0)。

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

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

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