I am trying to use an ArrayList to store my user-defined objects yet I keep coming up with the same problem...
Code:
public static void main()
{
ArrayList list = new ArrayList();
list.add(new item("Fred", 100));
list.add(new item("Jim",200));
System.out.println(((item)(list.get(0)).getIdentity());
}
When I run that code, the output is...
JIM
but of course is should be fred, is this a problem with referencing, it seems like the last item I add is overwriting the reference or data of the object(s) in the list. Can anyone suggest a solution or at least an explanation of the problem.
Freddy_no_nose.
Win if you can, Lose if you have to. BUT ALWAYS CHEAT.
Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
Posts
180
Try copying your .java files (not the .class files) into another directory, then compile and run from there.
"'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."
Is your item class possibly be static in your declaration? since you did not include the item class I'm not quite sure though, but the problem really lies there..static is the keyword.
Which, btw, means that they exists only once and are shared between all instances of the class. What you write out is actually the first class instance, but as all of them share the same data the second constructor call has simply overwritten it.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.