This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes multiple objects in a vector... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multiple objects in a vector..." Watch "multiple objects in a vector..." New topic
Author

multiple objects in a vector...

amr amr
Greenhorn

Joined: Mar 27, 2004
Posts: 6
hey i hope you can help me with this...,
I have a vector which contains several elements consisting of a user defined object. Now I'd like to add objects of ANOTHER user defined object. I know you can do this in C++, but can you also do this in java? My main question is once I add elements of this new objectt ype, how do I go through the vector element by element checking to see what type of object is stored in there. If I have the first user defined object in the slot, then I need to do a certain set of operations to it and if I have the 2nd user defined object in the slot then I need to do another set of operations to it....thanks for all the help!
amr
Jean-Francois Briere
Ranch Hand

Joined: Mar 03, 2004
Posts: 101
amr amr
Greenhorn

Joined: Mar 27, 2004
Posts: 6
thanks for the help but it still hasnt answered my question...perhaps I wasnt clear...I want to know how exactly you check to see what type of object is stored in the element...in other words in those if statements in the code how do i determine if elm is an instance of class1 or class2....i hope this clarifies my question better...thanks again...
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by amr amr:
thanks for the help but it still hasnt answered my question...perhaps I wasnt clear...I want to know how exactly you check to see what type of object is stored in the element...in other words in those if statements in the code how do i determine if elm is an instance of class1 or class2....i hope this clarifies my question better...thanks again...


To check the type of an object use the instanceof operator, I guess you missed it up in Jean-Francois post.

Originally posted by Jean-Francois Briere:

// verify its class type
if (elm instanceof ClassName1)
{
ClassName1 elm1 = (ClassName1)elm;
...
}

[ March 28, 2004: Message edited by: Vicken Karaoghlanian ]

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
amr amr
Greenhorn

Joined: Mar 27, 2004
Posts: 6
oh ok thanks, i thought it was pseudocode or something, i will try that...do i also need the elm before instane of that he has in the if statement
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: multiple objects in a vector...