I am trying to add objects of a class to a vector. But on adding the second object the first objects gets replaced with the second object.
Here is my code:
Vector res = new Vector();
Item item = new Item();
item.Name = "maclean";
item.Des = "abcd";
item.Price = "RS 350";
item.image = "c.gif";
res.add(item);
item.Name = "pinto";
item.Des = "gief";
item.Price = "RS 450";
item.image = "d.gif";
res.add(item);
for (int i = 0; i < res.size(); i++) {
item = (Item) res.get(i);
System.out.println(item.Name);
}
Here is my implementation of the Item
class used above.
public class Item {
public String Name=null;
public String Price=null;
public String Des=null;
public String image=null;
}
Output that I am getting:
pinto
pinto
Desired Output:
maclean
pinto
The above is a simple version of what I am trying to do.
Hi had forgot to mention earlier. I cannot create a new instance every time. Can any one suggest a suggestion so that i do not need to create a new instance every time. I want to know whether there is some structure other than vector which could store the entire object instead of pointer to the object.
Is there any structure readily available which i could use for my requirement. Or should a create a new array of the class objects.