我总是得到对象的arraylist的最后一个值 [英] I always get the last value of an arraylist of objects

查看:106
本文介绍了我总是得到对象的arraylist的最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想打印对象的arrayList的所有项目。但它只打印最后一项的值。



我尝试过:



这是我的代码:

Hello, I want to print all the items of an arrayList of objects. But it only prints the last item's values only.

What I have tried:

Here is my code:

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"));
}

// this is the items class: 

    import java.util.*;
    
    public class items {
    
    	String boxNumber;
    	public static Map<string,> dimension = new HashMap<string,>();
    	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,> getDimension() {
    		return dimension;
    	}
    
    	public static void setDimension(Map<string,> 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");
    	}
    }





这是我一直得到的:





And this is what I always get:

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





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

谢谢。



Can anyone please tell what is wrong with my code?
thank you.

推荐答案

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



您已将声明为 static ,表示项目列表中的每个对象将使用相同的维度值。我还注意到你的构造函数计算了它并存储它,但你的 getVolume 方法再次计算它。第一个计算是多余的,你应该只使用第二个计算。


You have declared dimension as static, which means that each object in the items list will use the same dimension values. I notice also that your constructor calculates the volume and stores it, but your getVolume method calculates it again. The first calculation is redundant, you should use the second one only.


我只是改变了维度:

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



to:

public Map< string,> dimension = new HashMap< string,>();
I just changed the dimension from:
public static Map<string,> dimension = new HashMap<string,>();

to:
public Map<string,> dimension = new HashMap<string,>();


这篇关于我总是得到对象的arraylist的最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆