An Iterable is a simple representation of a series of elements that can be iterated over. It does not have any iteration state such as a "current element". Instead, it has one method that produces an Iterator.
An Iterator is the object with iteration state. It lets you check if it has more elements using hasNext() and move to the next element (if any) using next().
Typically, an Iterable should be able to produce any number of valid Iterators.
An Iterable is a simple representation of a series of elements that can be iterated over. It does not have any iteration state such as a "current element". Instead, it has one method that produces an Iterator.
An Iterator is the object with iteration state. It lets you check if it has more elements using hasNext() and move to the next element (if any) using next().
Typically, an Iterable should be able to produce any number of valid Iterators.
An implementation of Iterable is one that provides an Iterator of itself:
public interface Iterable<T>
{
Iterator<T> iterator();
}
An iterator is a simple way of allowing some to loop through a collection of data without assignment privileges (though with ability to remove).
public interface Iterator<E>
{
boolean hasNext();
E next();
void remove();
}
See Javadoc.
I'm somewhat confused about difference between Iterator and Iterable
So what is Iterable in Java? Confused with it and Iterable.
I'm somewhat confused about difference between Iterator and ...
Need help understanding/writing Iterator and Iterable
so I know how to implement (e.g public Iterator<E> iterator() { return new ListIterator<E>(//could add first element here); }
but sometimes I see it saying public iterable. what's the difference? I think it is iterator is like you go through it once and you're done but iterable you get back a list that you can go through over and over again?
if it helps the thing I'm having trouble about is this: http://coursera.cs.princeton.edu/algs4/assignments/8puzzle.html
public Iterable<Board> neighbors()
do I just have to return a linked list of some sort (stack/queue)? why not use iterator<Board> instead?
Edit: I got it, you're returning an object that is iterable itself.
I just had to implement this to iterate through some private data structure. What threw me off is I couldn't index or do checks like I would with an array or list. I understand in general it is exactly like it sounds- used to iterate over some collection. Also that it is something we take for granted such as when using a foreach. That is all I get about it, it just seems like an inconvenience right now...