I am still quite new to Java and today I try to understand the use of iterators.
So I have a few questions:
Why would I need to implement Iterable at all, it only gives a method to create a new iterator for a collection, but I could just as well use a normal, or enchanced for-loop if I wanted to get all elements. I already read this question: What is the Iterable interface used for? and much more, but they kind of just say that it allows you to iterate through all your elements, which leads back to my question.
If I implement Iterable, should I implement Iterator too?
Because this one actually provides methods that could be useful to overwrite.
For example if my class had a boolean whether I want my objects in a loop or not, I could writepublic boolean hasNext() { if(loop) return true; //other things }
If I do like said in 2., will an enchanced for-loop use these overwritten methods?
For example with the mentioned loop attribute above: Would the for-loop run infintely if it was true?
I hope someone can help me better understand this whole concept.