aspose file tools*
The moose likes Beginning Java and the fly likes Using add on Vector<?> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using add on Vector<?>" Watch "Using add on Vector<?>" New topic
Author

Using add on Vector<?>

Bobby Anderson
Ranch Hand

Joined: Oct 28, 2008
Posts: 114
I am running into a problem calling add on a Vector<?>.

I.E.
Vector<?> v = new Vector();
v.add(new Integer(1));
v.add(new String( "Hello" ));

I guess what is the purpose of using <?> as a parameter? Does this mean I have to cast my vector to a specific type of vector before adding:
((Vector<Integer> ) v ).add(new Integer(1));

In that case why not just use Vector<Object> to aviod the cast.

Am I missing something?

Please explain.

Thanks
Billy

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

I guess what is the purpose of using <?> as a parameter?


The purpose of <?> is to specify that you don't know what type of elements goes into the vector. And since you don't know what type of element goes into a vector, there is no way for you to add anything.

Does this mean I have to cast my vector to a specific type of vector before adding:
((Vector<Integer> ) v ).add(new Integer(1));


No.... If you cast, then you can break the collection -- meaning that you can add something into the collection that you shouldn't be adding into the collection. Bottom line... you can't add anything (besides null) into that collection, you can only iterate and use items of the collection.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
When creating a collection containing "unknown types", you cannot add objects to it. This is meant to serve as a parameter for methods, like this:



When you do want to add anything that is an object, you'll have to give <Object> as the generic type.
But just for the record, are you planning to store various types (String, Integer, etc.) in one collection? This is generally not a good idea. Perhaps you would like to explain what "bigger" problem you're trying to solve? Perhaps I, or someone else can propose a different solution to it.

Good luck.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Henry Wong wrote:
I guess what is the purpose of using <?> as a parameter?


The purpose of <?> is to specify that you don't know what type of elements goes into the vector. And since you don't know what type of element goes into a vector, there is no way for you to add anything.
...


Except a null, which can be added to a Collection<?>.
But one could argue that null is nothing (or isn't anything). ; )
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Billy Newman wrote:In that case why not just use Vector<Object> to aviod the cast.

If you need a Vector (or any Collection implementation) in which you need to store objects that are not related at all, like Integer and String in your example, then Vector<Object> is the only option.

You should always make the generic type as tight as possible (e.g. use Vector<Integer> if you need to store only Integers, Vector<Number> if you can store any number etc), but sometimes as tight as possible simply is also as broad as possible: Vector<Object>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: Using add on Vector<?>