Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what's in my Vector? 1

Status
Not open for further replies.

joelmac

Programmer
Jun 4, 2002
92
0
0
CA
hey there,

If I put many different objects in a Vector, is there a way to tell what type of object it was before going into the Vector so that i can cast it back to that object and then do something different for each object type? ________________________
JoelMac
ICQ#:48144432
 
You will need to use the instanceof operator as you get elements out of your Collection. This type of problem is typically caused by bad design. I suggest reworking your application design so that you are only storing one type of item in the Vector.

This is exactly the type of problem that will be addressed by generics in Java 1.5. Can't wait.
 
hmm interesting, I'm glad you mentioned that that was bad design, I'm new at this. Maybe you, or someone else, could help me fix that.

Here's the simplified version of what i am trying to do:
I have a whole bunch of data stored in one table in a database. Each item is joined to a Group in a Groups table which stores information about the different groups of data. It gets a little tricky because each group may or may not be part of another group, so there's a self join.

I want to store each of the groups information in classes and then put all the classes in a Vector so i can easily list them, but since there can be multiple sub groups, I'm not really sure how to store it so that it is organized, and doesn't require a complex loop every time I want to display the group hierarchy (which will be on a web page).

I was thinking that I could store a group's subGroups in a vector and then add those vectors to a vector, so I would have Vector and Group objects all in the same Vector, but there must be a better way. Is there some other object I could use besides a Vector perhaps?
________________________
JoelMac
ICQ#:48144432
 
Just add a vector field to your base class.This vector field will get your sub data.Your main data is your class itself. Salih Sipahi
Software Engineer.
City of Istanbul Turkey
openyourmind77@yahoo.com
 
hey cool, i didn't think it would be so simple, Thanks ________________________
JoelMac
ICQ#:48144432
 
one more question:

what would be the easiest way to get all the infromation out of the table in the database and into the proper object or subObject? ________________________
JoelMac
ICQ#:48144432
 
I may suggest use Hashtable because in that we have key and value pairs for each key value we can store any type of object
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top