Implement Java Iterator and Iterable in same class?

后端 未结 4 1676
终归单人心
终归单人心 2020-12-13 21:03

I am trying to understand Java Iterator and Iterable interfaces

I am writing this class

class MyClass implements Iterable&         


        
4条回答
  •  离开以前
    2020-12-13 21:14

    You should not do Myclass implements Iterable,Iterator{ since iterators are single-use. With the exception of list iterators, there's no way to return them to the start.

    Incidentally, you can skip the

    MyClass myClass;
    public MyClassInterator(MyClass m){
      myclass=m;  
    }
    

    and instead of referencing

    myClass
    

    reference

    MyClass.this
    

    Your inner class is not static, so MyClass.this will reference the instance of the enclosing class that created it.

提交回复
热议问题