Reputation: 5975
Is it possible to convert:
ArrayList<?> objects;
to ArrayList<String>()
if you know objects
only contains Strings?
Upvotes: 1
Views: 77
Reputation: 1774
ArrayList<?> anonymList = null;
ArrayList<String> strings = (ArrayList<String>) anonymList;
Upvotes: 2
Reputation: 887365
Yes; just cast it.
You will get a compile-time warning, because even though you know that, the compiler doesn't.
Beware that if someone else adds some other class to the list after you cast it, you will get exceptions when you try to use it.
This is only true because of type erasure.
Upvotes: 7