Java – “iteratable cannot be converted to list” – is not an “iteratable” of type “list”?

I called a getelements method that returns iteratable < element >

I do this:

List<Element> elements = (List<Element>) getElements();

This results in an error:

java.lang.ClassCastException: com.utesy.Element$3 
cannot be cast to java.util.List

I think lists are a type of iteratable?

Solution

Yes, the list < T > extends iteratable T, but that doesn't mean you can list < T > from any repeatable < T > – only if the value actually references an instance of the type of list < T > It is entirely possible to implement Iterable < T > without implementing the rest of the list < T > Interface... What happens to you in this case?

For simplicity, let's change iteratable < T > to object and list < T > to string String extends object, so you can try to convert from object to string... But if the reference actually references string (or null), the conversion will only succeed at execution time

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>