Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
Java
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.
Posted
Updated 25-Feb-16 23:42pm
v2
Comments
Krunal Rohit 26-Feb-16 5:34am    
Can you debug and find the exact spot at where the error is ?

KR
Rii93 26-Feb-16 5:40am    
I already found the problem .
I just changed the dimension from:
public static Map<string, float=""> dimension = new HashMap<string, float="">();

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

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

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.
 
Share this answer
 
Comments
Rii93 26-Feb-16 5:44am    
yes , I understand .
Thank you!
I just changed the dimension from:
public static Map<string,> dimension = new HashMap<string,>();

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900