Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-23-2008, 01:15 AM
Member
 
Join Date: Apr 2008
Posts: 2
Nathand is on a distinguished road
Can I use vectors to store multiple types of objects
Can I use vectors to store multiple types of objects? Say I have two classes, cat & dog. Could someone show me an example of how to create a vector, and then add instances of each object type to the vector?


Thanks so much,

Nathand
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-23-2008, 01:40 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,659
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Yes you can. Just call add method for different kinds of objects. While getting the object from the vector, you can use instanceof operator to test the type of the object and cast accordingly:

Code:
Vector v = new Vector(); v.add(new Cat()); v.add(new Dog()); if (v.elementAt(0) instanceof Cat) { Cat cat = (Cat)v.elementAt(0); }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-23-2008, 07:42 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
I have the feeling that he might mean array not Vector. But the answer is the same: create an array of Object (Object[]) and add whatever you want to it. When you need them from the array check with instanceof.
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-23-2008, 11:53 PM
Member
 
Join Date: Apr 2008
Posts: 2
Nathand is on a distinguished road
Thanks so much!! That's exactly the kind of example I needed. I did mean vector instead of array, I want to use a vector because it grows sort of dynamically where with an array I have to copy things a bunch of times.

Thanks again!

Nathand
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-24-2008, 12:26 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
Hi Nathand,

Sorry for making assumptions about what you wanted but I am used to be kind of precise in what I say. I would've say java.util.Vector or at least Vector ;-)
One more comment - usually checking for instanceof is not a sign of a good design. In OOP you have the mechanisms to write generic code that doesn't need to check. It is way better to find a solution that is object oriented and polymorphic. For example in the above example you should strive to make everything you put in the Vector or array derived from a certain class or implementing a certain interface and define the Vector or the array as being of that type using generics. Then when you process the Vector each instance will know what to do on its own. But I am sure you know the theory ;-)
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-24-2008, 09:48 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
Generics
You should use Generics when doing this. This way you remove a lot of run time errors to complier errors.

What you should do is.

Code:
Vector<Object Type> myVector = new Vector<ObjectType>();
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-28-2008, 09:55 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
instance of is a very powerful tool to use. That is why its there. Generics helps get rid of unneeded use of it though
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vectors of Vectors or hash-somethings? mindwarp New To Java 3 03-10-2008 04:57 PM
Converting data types bluekswing New To Java 4 01-12-2008 02:48 AM
Types of JDBC drivers JavaForums Java Blogs 0 12-25-2007 03:21 PM
Displaying different image types splinter64uk AWT / Swing 1 12-05-2007 10:12 AM
Using enum types (with example) JavaForums Java Blogs 0 11-17-2007 04:43 AM


All times are GMT +3. The time now is 09:10 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org