Results 1 to 7 of 7
  1. #1
    Nathand is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    9

    Default

    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:

    Java 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);
    
    }

  3. #3
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    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 @ [www.littletutorials.com]
    Language is froth on the surface of thought

  4. #4
    Nathand is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default

    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

  5. #5
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    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 @ [www.littletutorials.com]
    Language is froth on the surface of thought

  6. #6
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default 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.

    Java Code:
       Vector<Object Type> myVector = new Vector<ObjectType>();

  7. #7
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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

Similar Threads

  1. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 02:57 PM
  2. Converting data types
    By bluekswing in forum New To Java
    Replies: 4
    Last Post: 01-12-2008, 12:48 AM
  3. Displaying different image types
    By splinter64uk in forum AWT / Swing
    Replies: 1
    Last Post: 12-05-2007, 08:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •