Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vectors and casting....

 
James Lechte
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a vector of objects which i need do work with. The way in which i can pull an object is via one of its attributes, not its index so i thought i'd use an Iterator.
however once the object is found ie.(serial number a string)
object anObject = i.next();
if(anObject.getSerialNumber().equals(inSerialNumber)
{

once i edit the 'anObject' it is only a copy of the object in i.next() - my question is, what is the easiest way to copy the edited object 'anObject' back into the vector over the old object, as vertor class requires a int index to add or remove or setElementAt.....
thanks in advance for help!!!
 
Graeme Brown
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call i.next() what you get is a copy of the reference to the object in the Vector, not a copy of the object itself, unless you then clone the object which I assume you are not doing.
Therefore if you amend the data in the object this change will be reflected in the Vector, there is no need to put the object back into the Vector.
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
If you want to get an object via an attribute in the object, then I would suggest you use a HashMap as your collection/list. This way you can store an object and use the 'serial' number as the key.
Makes it MUCH easier to get your values, and you don't have to iterate through your whole Vector over and over again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic