“Iterable cannot be cast to List” - Isn't `List` a type of `Iterable`?

后端 未结 10 1676
生来不讨喜
生来不讨喜 2021-02-20 00:27

I called a getElements method which returns Iterable.

I did this:

List elements = (List

        
10条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-20 01:06

    List extends Collection which in turn extends Iterable. You therefore trying to cast to a subtype which won't work unless getElements() really is returning a List (which the signature doesn't in any way guarantee).

    See: http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html

提交回复
热议问题