我总是得到对象的arraylist的最后一个值

时间:2016-02-26 10:00:07

标签: java arraylist hashmap

我想打印一个arrayList对象的所有项目。但它只打印最后一项的值。 这是我的代码:

List<items> boxes = new ArrayList<items>();
boxes.add(new items("1", 0.1f, 0.2f, 0.1f));
boxes.add(new items("2", 0.1f, 0.4f, 0.1f));
boxes.add(new items("3", 0.1f, 0.1f, 0.2f));
boxes.add(new items("4", 0.1f,0.1f, 0.3f));
boxes.add(new items("5", 0.2f, 0.1f, 0.1f));
boxes.add(new items("6", 0.1f, 0.1f, 0.1f));
boxes.add(new items("7", 0.2f, 0.3f, 0.1f));
boxes.add(new items("8", 0.1f, 0.3f, 0.1f));
boxes.add(new items("9", 0.2f, 0.2f, 0.2f));

for (items box: boxes){
    System.out.println();
    System.out.println("length:  " +box.dimension.get("length"));
    System.out.println("breadth:  " +box.dimension.get("breadth"));
    System.out.println("height:  " +box.dimension.get("height"));
}

这是项目类:

import java.util.*;

public class items {

    String boxNumber;
    public static Map<String, Float> dimension = new HashMap<String, Float>();
    public double volume;

    public items(float l, float b, float h) {
        volume = l * b * h;
        dimension.put("length", l);
        dimension.put("breadth", b);
        dimension.put("height", h);

    }

    public items(String boxName, float i, float j, float k) {
        boxNumber = boxName;
        volume = i * j * k;
        dimension.put("length", i);
        dimension.put("breadth", j);
        dimension.put("height", k);

    }

    public static Map<String, Float> getDimension() {
        return dimension;
    }

    public static void setDimension(Map<String, Float> dimension) {
        items.dimension = dimension;
    }

    public items rotateBox() {

        Set<String> keySet = this.dimension.keySet();
        String[] sides = keySet.toArray(new String[3]);
        dimension.put(sides[1],
                dimension.put(sides[2], dimension.get(sides[1])));
        dimension.put(sides[0],
                dimension.put(sides[2], dimension.get(sides[0])));

        return this;
    }

    public double getVolume() {
        // TODO Auto-generated method stub
        return dimension.get("length") * dimension.get("breadth")
                * dimension.get("height");
    }
}

这就是我一直得到的:

length:  0.2
breadth:  0.2
height:  0.2

length:  0.2
breadth:  0.2
height:  0.2

length:  0.2
breadth:  0.2
height:  0.2

length:  0.2
breadth:  0.2
height:  0.2

length:  0.2
breadth:  0.2
height:  0.2

length:  0.2
breadth:  0.2
height:  0.2

length:  0.2
breadth:  0.2
height:  0.2

length:  0.2
breadth:  0.2
height:  0.2

length:  0.2
breadth:  0.2
height:  0.2

有人可以告诉我的代码有什么问题吗?

2 个答案:

答案 0 :(得分:3)

您已将dimension声明为静态变量which means it will be shared by all instances of your items class

public static Map<String, Float> dimension = new HashMap<String, Float>();

因此,每次创建新实例时,地图中的值都会被

覆盖
dimension.put("length", l);
dimension.put("breadth", b);
dimension.put("height", h);

因此,您将获得的最终值是您插入的最后一项的值。

您应该使用以下内容将其设为实例变量:

public Map<String, Float> dimension = new HashMap<String, Float>();

作为旁注:

  • 您应该更喜欢private实例变量而非公共变量,并且有getter。
  • 考虑尊重Java命名约定:items类应为Items

答案 1 :(得分:0)

您已将HashMap维度作为静态变量,因此它将保留其值并由items类的所有实例共享。 通过将Map声明为

,将Map作为成员变量
    public Map<String,Float> dimension = new HashMap<String,Float>();

此成员变量将特定于Class项的每个实例,并且可以使用&#34;此&#34;关键字从构造函数访问。例如: -

    public items(String boxName, float i, float j, float k) {
        boxNumber = boxName;
        volume = i * j * k;
        this.dimension.put("length", i);
        this.dimension.put("breadth", j);
        this.dimension.put("height", k);
    }