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.
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.