对象类和数组 – 为什么它返回’null’? 【JAVA]

我写了一个小类,创建一个包含3个数组的报表对象。 在创建对象时,这些数组使用值进行初始化。 但是,当我测试类以查看departments数组中的内容时,它会打印出数组元素为null。 为什么?

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 

谢谢

巴巴

就这样吧

 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 } 

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

但是,如果将声明与填充分开,则填充代码会有所不同,并且它们没有那么正确。 你还有一个不属于的’+’字符。

此代码编译和工作(测试),基本上完成您的寻找。

 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将提供一个空的。