Reputation: 4266
I have a Vector
like so:
Vector myVector = new Vector();
And an ArrayList
of a custom class which contains n
amount of String
s.
Basically, I add an empty item into the first position in the Vector (myVector.insertElementAt("", 0);
), and then add (usually) the rest of the items from the ArrayList
(depending on the needs). This Vector
then is used as the the DefaultComboBoxModel
for a JComboBox
.
Now, I get the usual warning References to the generic type Vector should be parameterized.
The items are of type String
, but I can't use Vector<String>
because adding the items from the ArrayList
(using .add()
) won't work as:
The method add(String) in the type Vector(String) is not applicable for the arguments (CustomClass).
I can't use Vector(CustomClass)
because then insertElementAt
throws a wobbler.
So my question is: Is it safe to just use Vector(Object)
or not parameterize the type at all?
I could use T
and cast everything but would get a type safety error.
Upvotes: 0
Views: 65
Reputation: 140319
I can't use Vector because adding the items from the ArrayList (using .add()) won't work as:
The method add(String) in the type Vector(String) is not applicable for the arguments (CustomClass).
So, you are trying to add things from an ArrayList<CustomClass>
to a Vector
that you expect only to contain String
elements.
If you are using a raw-typed Vector
, you can do this, but it will fail down the line if you try to do anything with an element from that Vector
that you treat as a String
, because it's not.
If you want the Vector
to contain String
s, add a String
:
myVector.add(thingFromArrayList.toString());
(or some other means of converting thingFromArrayList
to a String
)
and then you can add the <String>
to myVector
's type.
Upvotes: 1