Results 1 to 6 of 6
  1. #1
    Excession is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Question Multiple types in Vector - type checking

    Hi all.

    I've got a Vector that can hold two types of object - either a standard String or a dataHolder object that contains a string and has it's own to string method. (The reason is that the vector holds a list of addresses as strings - the dataHolder is a mutable string object that allows modifcation of the addresses).

    The problem is the type checking. If I use Vector<String> myVector the compiler won't accept the data holder object. If I use Vector<dataHolder> the compiler won't accept Strings.

    Is there a way to tell the compiler that the vector can contains either a string or a dataholder object?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You could use Vector without the generic definition:
    Java Code:
    private Vector myFlexVector = new Vector()
    but having said that, what you are trying to do -- have a container hold different types of class of objects -- is usually a symptom of bad program design. Why do you feel it is necessary for your program to do this?

  3. #3
    Excession is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi there - if I use a generic vector as you suggest then I get the "code uses unsafe or unchecked operations" message.

    The two types of object I want the vector to hold are java.lang.string and a mutable string class I have created. They are all in reality strings.

    e.g.

    Java Code:
    private class address {
            private String location;
    
            public <String> address(String location){
                this.location= location;
            }
            
            public void setLocation(String location){
                this.location= location;
            }
            public String getLocation(){
                return location;
            }
            @Override
            public String toString(){
               return location; 
            }
        }
    and then

    Java Code:
    Vector<String> myVector = new Vector<String>();
    Address myAddress = new Address("here");
    myVector.add("some normal string");  // add normal string
    myVector.add(myAddress.getLocation);// add mutable string

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,898
    Rep Power
    25

    Default

    Interesting problem. How have others handled it?
    You want to use Generics with a class that allows more than one type to be stored in it. The add() part could be handled by overriding:
    add(type1) or add(type2).

    But how to do the get()? Which of the 2 types is returned?
    Since you don't know before hand which type you'll get back, the only way to detect it would be with instanceof. get() would have to return an Object or some new wrapper class you've written that could sort out between the 2 types.
    If you know what type you are getting, then you could extend Vector and have separate get()s for each type that would return the specific type.

    Another idea:
    Wrap Vector with a sort of pass thru class with a peek ahead method that will tell you the type of the next object to be returned by a get() then have a get() that returns that type.
    implementation left to you
    Last edited by Norm; 07-13-2008 at 06:05 PM.

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    A bag of fruit is not a bag of apples.

    If you use type free collections, you get an unsafe warning (by default) because what you are doing is unsafe. You can turn off the warning using annotations. But a better approach is to not use untyped collections.

    Ask: why does your Vector (or any other collection) needs to have heterogenous items? This is generally bad design. If you really need to handle both, create a holder class, and put the items into instances of the holder, and have the Vector be typed to the holder objects.

    You nearly always want the collections to be homogeneous so you can do stuff like
    Java Code:
       for (Foo f : list) {
          f.baz();
      }

  6. #6
    Excession is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    I think the holder class is the way i'm going to have to do it - it just means quite a bit of refactoring and i was hoping there was another simpler solution.

Similar Threads

  1. Open type (Ctrl-Shift-T) does not find types
    By Zhenya_Merom in forum Eclipse
    Replies: 2
    Last Post: 11-04-2009, 11:15 AM
  2. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 11:43 AM
  3. Hastable - Multiple Types
    By sopna sajith in forum Advanced Java
    Replies: 3
    Last Post: 06-29-2008, 05:40 AM
  4. Can I use vectors to store multiple types of objects
    By Nathand in forum Advanced Java
    Replies: 6
    Last Post: 04-28-2008, 08:55 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
  •