Reputation: 6925
I have some function that requires a List<SomeType>
and I have an Object from that I know that it should be of type ArrayList<SomeType>
.
How can I test if it really is and how can I cast the object to that type without getting warnings?
something like:
ArrayList<String> testL = new ArrayList<String>();
Object o = testL;
if (o instanceof ArrayList<String>){
List<String> l = (ArrayList<String>)o;
}
The instanceof
check gives an error and the cast gives an [unchecked]
warning.
Upvotes: 2
Views: 1306
Reputation: 729
What you are trying to do cannot be achieved by one simple cast. This has to do with type erasure during compilation of Java code. You can do one of two things:
Upvotes: 6