对象类和数组 - 为什么它返回“null” ? [java]

发布于 2024-10-02 18:20:42 字数 1146 浏览 2 评论 0原文

我编写了一个小类,它创建一个包含 3 个数组的报表对象。在创建对象时,这些数组用值进行初始化。然而,当我测试该类以查看例如部门数组中的内容时,它会打印出数组元素为空。为什么?

 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

测试班

 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

谢谢巴巴

I wrote a small class that creates a report object containing 3 arrays. 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. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新一帅帅 2024-10-09 18:20:42

使它像这样

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  

实际上,您正在创建构造函数本地的新数组对象,这些对象在构造函数中进行初始化。

您的类字段将使用上面的代码进行初始化。

如果您没有错误地完成此操作,请参阅此文档,它也会更好地阐明您的理解

**

更新

:**
上面的代码会给你非法的表达式开始

这是工作代码

 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
 } 

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
 } 
迷离° 2024-10-09 18:20:42

正如其他答案所指出的,您的构造函数创建了新的局部变量“隐藏”实例变量,而不是用数据填充实例变量。

但是,如果将声明与填充分开,则填充代码会略有不同,并且他们没有完全正确地理解这一点。您还有一个不属于的“+”字符。

该代码可以编译并运行(经过测试),并且基本上可以满足您的要求。

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

您也可以在声明中创建数组,然后在构造函数或其他地方填充数组条目。

使用这种方法,代码可能类似于:

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

作为第三种选择,您可以在字段声明中执行所有初始化,如下所示:

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

在这种情况下,您根本不需要定义构造函数,因为它什么也不做。 Java 将提供一个空的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文