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