wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Vectors and casting.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Vectors and casting...." Watch "Vectors and casting...." New topic
Author

Vectors and casting....

James Lechte
Ranch Hand

Joined: Mar 02, 2001
Posts: 30
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

Joined: Oct 13, 2000
Posts: 193
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

Joined: Mar 09, 2000
Posts: 320
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.

SOURCE CODE should be SURROUNDED by "code" tags.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vectors and casting....
 
Similar Threads
Problem when using ternary operatory in For Each loop
local variable thread safe?
clarification needed
JComboBox.setSelectedItem() does not update the displayed value