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

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

I called a getElements method which returns Iterable.

I did this:

List elements = (List

        
10条回答
  •  北荒
    北荒 (楼主)
    2021-02-20 01:27

    List is a subinterface of Iterable meaning that List includes pretty much everything that Iterable has, however not the other way around. So not all methods in a List instance would have an equivalent in Iterable.

    Try to avoid that sort of casting.

    I would recommend you to take a quick look at the Java 6 API and the tutorials covering casting

提交回复
热议问题