Results 1 to 6 of 6
- 07-13-2008, 11:44 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
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?
-
You could use Vector without the generic definition:
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?Java Code:private Vector myFlexVector = new Vector()
- 07-13-2008, 04:36 PM #3
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
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.
and thenJava 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; } }
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
- 07-13-2008, 04:54 PM #4
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 youLast edited by Norm; 07-13-2008 at 05:05 PM.
- 07-13-2008, 06:58 PM #5
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(); }
- 07-13-2008, 07:06 PM #6
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Open type (Ctrl-Shift-T) does not find types
By Zhenya_Merom in forum EclipseReplies: 2Last Post: 11-04-2009, 10:15 AM -
[SOLVED] Cast string type to int type
By GilaMonster in forum New To JavaReplies: 9Last Post: 09-17-2008, 10:43 AM -
Hastable - Multiple Types
By sopna sajith in forum Advanced JavaReplies: 3Last Post: 06-29-2008, 04:40 AM -
Can I use vectors to store multiple types of objects
By Nathand in forum Advanced JavaReplies: 6Last Post: 04-28-2008, 07:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks