简体   繁体   English

为什么我的二维数组打印全0?

[英]why does my two-dimensional array print all 0s?

I have a 2-D array named Maze. 我有一个名为迷宫的二维数组。 it is a global variable. 它是一个全局变量。 I hard coded the data at each position in my setData() method. 我在setData()方法的每个位置上对数据进行了硬编码。 but when I try to print my array down below it prints all 0s. 但是当我尝试将数组向下打印时,它会打印全0。 why is that? 这是为什么? thank you. 谢谢。

public class Maze
{
    int [][] maze = new int[4][6];
    public void setData()
    {
        maze[0][0] = 1; maze[0][1] = 0; maze[0][2] = 1; maze[0][3] = 1; maze[0][4] = 0; maze[0][5] = 1;
        maze[1][0] = 0; maze[1][1] = 0; maze[1][2] = 1; maze[1][3] = 0; maze[1][4] = 0; maze[1][5] = 0; 
        maze[2][0] = 1; maze[2][1] = 0; maze[2][2] = 1; maze[2][3] = 0; maze[2][4] = 1; maze[2][5] = 0; 
        maze[3][0] = 0; maze[3][1] = 0; maze[3][2] = 0; maze[3][3] = 0; maze[3][4] = 1; maze[3][5] = 1; 
    }  

    public void printMaze()
    {
        System.out.println("The matrix: ");

        for (int m=0; m<4; m++)
        {
            for (int n=0; n<6; n++)
                System.out.print(maze[m][n]);
            System.out.println();
        }
    }
}

public class MazeTester 
{
    public static void main (String [] args)
    {
        Maze mmgg = new Maze();

        mmgg.printMaze();
    }
}

You need to run setData to put all your values in. 您需要运行setData来放入所有值。

   public static void main (String [] args)
   {
      setData();
      printMaze();
   }

您永远不会调用setData()方法,因此该数组将充满默认值0。

Your array prints all 0s because you never initialize it (you just declared setData() and you didn't call it), thus its elements get default values which are 0 for int variables. 您的数组将输出全0,因为您从未初始化过它(您刚刚声明了setData()且未调用它),因此其元素的默认值对于int变量为0。

If you don't want to use the setData() method, you can initialize your array in an init bloc: 如果您不想使用setData()方法,则可以在init bloc中初始化数组:

     {
          maze[0][0] = 1; maze[0][1] = 0; maze[0][2] = 1; maze[0][3] = 1; maze[0][4] = 0; maze[0][5] = 1;
          maze[1][0] = 0; maze[1][1] = 0; maze[1][2] = 1; maze[1][3] = 0; maze[1][4] = 0; maze[1][5] = 0; 
          maze[2][0] = 1; maze[2][1] = 0; maze[2][2] = 1; maze[2][3] = 0; maze[2][4] = 1; maze[2][5] = 0; 
          maze[3][0] = 0; maze[3][1] = 0; maze[3][2] = 0; maze[3][3] = 0; maze[3][4] = 1; maze[3][5] = 1; 
      }  

In that way, you just have to instantiate your classe Maze and call the method printMaze(): 这样,您只需要实例化Maze类并调用方法printMaze():

  public static void main ( String[] args ) {

        Maze mmg = new Maze();
        mmg.printMaze ();

       }

Just as jnd said, you will need to invoke the setData() method on your object reference - mmgg, like so: 正如jnd所说,您将需要在对象引用-mmgg上调用setData()方法,如下所示:

mmgg.setData(); mmgg.setData();

Also, to answer your question the reason why it prints all 0s; 另外,要回答您的问题,为什么打印全0的原因? as you have observed is that upon instantiation of the object (this happens via the 'new' operator) the compiler sets your class's member variables to "reasonable" defaults. 如您所见,是在对象实例化时(通过'new'运算符发生),编译器将类的成员变量设置为“合理的”默认值。

Here are the defaults - which explain why your int values from your maze 2D array were zero after your Maze class was instantiated but without calling your setData() method... 这是默认值-解释了为什么在迷宫类实例化之后但不调用setData()方法的情况下迷宫2D数组中的int值为零的原因...

  1. byte [default = 0] 字节[默认= 0]
  2. short [default = 0] 短[默认= 0]
  3. int [default = 0] int [默认= 0]
  4. long [default = 0L] 长[默认值= 0L]
  5. float [default = 0.0F] 浮点[默认= 0.0F]
  6. double [default = 0.0D] double [默认= 0.0D]
  7. boolean [default = false] 布尔值[默认=假]
  8. char [default = '\'] 字符[默认='\\ u0000']
  9. String (or any Object) [default = null] 字符串(或任何对象)[默认= null]

[NB:- The compiler never assigns a default value to an uninitialized local variable] [NB:-编译器永远不会将默认值分配给未初始化的局部变量]

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

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