What is the Iterable interface used for?

前端 未结 5 1164
执念已碎
执念已碎 2020-12-08 10:01

I am a beginner and I cannot understand the real effect of the Iterable interface.

5条回答
  •  佛祖请我去吃肉
    2020-12-08 10:39

    It returns an java.util.Iterator. It is mainly used to be able to use the implementing type in the enhanced for loop

    List list = ...
    for (Item i:list) {
     // use i
    }
    

    Under the hood the compiler calls the list.iterator() and iterates it giving you the i inside the for loop.

提交回复
热议问题