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 07-13-2008, 01:44 PM
Member
 
Join Date: Jul 2008
Posts: 3
Excession is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-13-2008, 05:03 PM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 805
Fubarable is on a distinguished road
You could use Vector without the generic definition:
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?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-13-2008, 06:36 PM
Member
 
Join Date: Jul 2008
Posts: 3
Excession is on a distinguished road
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.

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

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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-13-2008, 06:54 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Gone to Costa Rica
Posts: 2,223
Norm is on a distinguished road
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 07:05 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-13-2008, 08:58 PM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 462
fishtoprecords is on a distinguished road
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
Code:
for (Foo f : list) { f.baz(); }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-13-2008, 09:06 PM
Member
 
Join Date: Jul 2008
Posts: 3
Excession is on a distinguished road
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.
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
[SOLVED] Cast string type to int type GilaMonster New To Java 9 09-17-2008 12:43 PM
Open type (Ctrl-Shift-T) does not find types Zhenya_Merom Eclipse 1 08-05-2008 11:54 AM
Hastable - Multiple Types sopna sajith Advanced Java 3 06-29-2008 06:40 AM
Can I use vectors to store multiple types of objects Nathand Advanced Java 6 04-28-2008 09:55 AM
Checking if Session is new JavaForums Java Blogs 0 03-31-2008 06:50 PM


All times are GMT +3. The time now is 03:52 PM.


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