简体   繁体   English

对象类和数组-为什么返回“ null”? 【JAVA]

[英]objects classes and arrays - why is it returning 'null' ? [java]

I wrote a small class that creates a report object containing 3 arrays. 我写了一个小类,创建一个包含3个数组的报表对象。 At creation of the object these arrays are initialised with values. 在创建对象时,将使用值初始化这些数组。 However when i test the class to see for example what's in the departments array, it prints out that the array elements are null. 但是,当我测试类以查看例如Departments数组中的内容时,它会打印出数组元素为null。 why? 为什么?

 class  Report
 {
        // declare instance variables (arrays)
        public String[] departments = new String[4] ;
        public double[] grossTotals = new double[4] ;
        public double[] taxTotals = new double[4]  ;


        // constructor
        public Report(){
            // declare, create and initialise all in one statement
            String[] departments = {"Accounting", "Sales", "HR", +
                                              "Administration"} ;
            double[] grossTotals = {0.0, 0.0, 0.0, 0.0} ;
            double[] taxTotals = {0.0, 0.0, 0.0, 0.0} ;

    } // END constructor
 } // class  Report

Test Class: 测试类别:

 class TestReport 
 {
        public static void main(String[] args) {
            // create report object
            Report r = new Report();

                for (int i = 0; i <= 3 ; i++ )
                {
                System.out.println(r.departments[i]) ;
                }

        } //end main
 } // end test class

thanks 谢谢

Baba 巴巴

Make it like this 像这样

public Report(){
            // declare, create and initialise all in one statement
            this.departments = {"Accounting", "Sales", "HR", +
                                              "Administration"} ;
            this.grossTotals = {0.0, 0.0, 0.0, 0.0} ;
            this.taxTotals = {0.0, 0.0, 0.0, 0.0} ;

    } // END constru  

Actually you are creating new arrays objects local to your constructor those are getting initialized in constructor. 实际上,您是在构造函数本地创建新的数组对象,这些对象将在构造函数中初始化。

your class fields will be initialized using the code above . 您的班级字段将使用上面的代码初始化。

If you have not done it by mistake than please refer to this doc also it will better clear your understandings 如果您没有误操作,请参考此文档,这样也可以更好地清除您的理解

** **

Update 更新

:** Above code will give you illegal start of expression :**上面的代码将给您非法的表达开始

Here is working code 这是工作代码

 class  Report
 {
        // declare instance variables (arrays)
        public String[] departments = null;
        public double[] grossTotals = null;
        public double[] taxTotals = null;


        // constructor
        public Report(){
              this.departments = new String[]{"Accounting", "Sales", "HR", "Administration"} ;
         this.grossTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
         this.taxTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
    } // END constructor
 } 

As the other answers have noted, your constructor created new local variables "shadowing" the instance variables instead of populating the instance variables with data. 正如其他答案所指出的那样,您的构造函数创建了新的局部变量,从而“遮蔽”了实例变量,而不是用数据填充实例变量。

However, the population code is a little different if you separate the declaration from the populating, and they didn't get that quite right. 但是,如果您将声明与填充项分开,则填充代码略有不同,并且它们并不太正确。 You also had a '+' character that didn't belong. 您还具有一个不属于的“ +”字符。

This code compiles and works (tested), and does basically what your looking for. 该代码可以编译和运行(经过测试),基本上可以完成您所需要的工作。

class  Report
{
       // declare instance variables (arrays)
       public String[] departments;
       public double[] grossTotals;
       public double[] taxTotals;


       // constructor
       public Report(){
           // populate instance variables
           departments = new String[]{"Accounting", "Sales", "HR",
                                             "Administration"} ;
           grossTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
           taxTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;

   } // END constructor
} // class  Report

You can alternatively create the arrays in the declaration and then populate the array entries in the constructor or elsewhere. 您也可以在声明中创建数组,然后在构造函数或其他地方填充数组条目。

With that approach the code could be something like: 使用这种方法,代码可能类似于:

class  Report
{
       // declare instance variables (arrays)
       public String[] departments = new String[4];
       public double[] grossTotals = new double[4];
       public double[] taxTotals = new double[4];


       // constructor
       public Report(){
           // populate instance variable entries
           departments[0] = "Accounting";
           departments[1] = "Sales";
           departments[2] = "HR";
           departments[3] = "Administration";

           for (int i = 0; i < 4; i++) {
               grossTotals[i] = 0.0;
               taxTotals[i] = 0.0;

           }
   } // END constructor
} // class  Report

As a third alternative, you can do all the initialization in the field declaration as follows: 作为第三种选择,您可以按如下所示在字段声明中进行所有初始化:

class  Report
{
       // declare and populate instance variables (arrays)
       public String[] departments = new String[]{"Accounting", "Sales", "HR",
                                             "Administration"} ;
       public double[] grossTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
       public double[] taxTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;

} // class  Report

In this case, you don't need to define the constructor at all, as it does nothing. 在这种情况下,您根本不需要定义构造函数,因为它什么也不做。 An empty one will be supplied by Java. Java将提供一个空的。

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

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