How can I make my class iterable so I can use foreach syntax?

后端 未结 5 1016
借酒劲吻你
借酒劲吻你 2020-12-16 10:33

I have Book and BookList classes. BookList is something like this:

public class BookList 
{
    private final List<         


        
5条回答
  •  甜味超标
    2020-12-16 11:18

    For making a class iteratable you need to implement Iterable Interface.

    a) Declare the class as below

    public class BookList implements Iterable
    

    b) Override the iterator() Method

    Read More. Hope it will help you.

提交回复
热议问题