【问题标题】:My Java object array only returns the last element我的 Java 对象数组只返回最后一个元素
【发布时间】:2018-02-27 19:25:58
【问题描述】:

我正在尝试完成一个 Java 实验室练习,该练习要求在数组中显示对象(圆柱体)的体积。每当我尝试打印数组时,输出似乎总是数组中的最后一个对象,而我希望它们都打印出来。

这是我的 Cylinder.java 代码:

public class Cylinder implements Comparable<Cylinder>  {

public static double radius;
public static double height;
public static String name;

public Cylinder(double radius, double height, String name){

    this.radius = radius;
    this.height = height;
    this.name = name;

}

@Override
public int compareTo(Cylinder obj) {

   Cylinder other = obj;
   int result;

    if (this.volume() > other.volume()){
         result = -1;
    }
    else if (this.volume() < other.volume()){
        result = 1;
    }
    else {
        result = 0;
    }
    return result;
}

public double volume(){
    double volume = Math.pow(radius, 2.0)*Math.PI*height;
    return volume;

}

public double surface(){
    double surface = (4.0*Math.PI)*Math.pow(radius, 2.0);
    return surface;

}

public String toString(){
    return "Name: " + name + ", radius: " + radius + ", height: " + height ;
    }
}

TestSolids.java,打印数组:

import java.util.Arrays;

public class TestSolids {
public static void testCylinderSort(Cylinder[] cylinders){

 for(int i = 0; i < cylinders.length; i++){
  double volume = cylinders[i].volume();
  System.out.println("Volume of Cylinder " + (i+1) + " " + volume);
  }
}

public static void main(String[] args){

    final Cylinder[] CYLINDERS = { new Cylinder(10, 5, "one"), new Cylinder(5, 10, "two"), new Cylinder(7, 7, "three") };
    System.out.println(Arrays.toString(CYLINDERS));
    testCylinderSort(CYLINDERS);
    }
}

我的输出:

[Name: three, radius: 7.0, height: 7.0, Name: three, radius: 7.0, height: 7.0, Name: three, radius: 7.0, height: 7.0]
Volume of Cylinder 1 1077.566280181299
Volume of Cylinder 2 1077.566280181299
Volume of Cylinder 3 1077.566280181299

输出显示我可以打印数组的不同索引,但由于某种原因,它们都引用了数组的最后一个元素,我不知道为什么会这样。如果有人能告诉我这里发生了什么以及如何打印所有数组对象,我将非常感激。

【问题讨论】:

  • 从变量声明中删除static
  • 啊!它总是那么简单,谢谢你的工作。为什么列出的是最后一个元素而不是第一个元素?

标签: java arrays object for-loop


【解决方案1】:

你声明的变量是静态的。

您需要删除静态,因为静态属于类而不是成员变量。因此,每次初始化圆柱体的值或半径时,后面的圆柱体值总是会覆盖前面的值。

使用:

public  double radius;
public  double height;
public  String name;

PS:

您可能希望将它们设为私有并公开 getter 和 setter 方法以获得更好的代码

【讨论】:

  • 我完全同意。 getter 和 setter 很重要,它们鼓励类型安全,而 Java 就是关于类型安全的。 +1
【解决方案2】:

您的实例变量被声明为static

public static double radius;
public static double height;
public static String name;

静态变量是该类的全局变量。

所以当你创建三个对象时

final Cylinder[] CYLINDERS = { new Cylinder(10, 5, "one"), new Cylinder(5, 10, "two"), new Cylinder(7, 7, "three") };

radiusheightname 变量不断被替换。因此,这些变量的值是最后设置的值。

new Cylinder(7, 7, "three")

这就是你认为的“迭代总是返回最后一个元素”。

实际发生的是返回了不同的对象,但它们具有相同的变量集(在类级别),而不是每个对象都有一组变量(在对象级别)。

解决方法是删除 static 关键字。

【讨论】:

    猜你喜欢
    • 2015-04-29
    • 2014-10-17
    • 2019-11-13
    • 2015-09-16
    • 1970-01-01
    • 2013-04-30
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多