IteratorはListIteratorのスーパークラスです。
これらの違いは次のとおりです。
- を使用
iteratorすると、前方にのみ移動できますがListIterator、要素を読みながらバックワードを移動することもできます。
- を使用
ListIteratorすると、トラバース中の任意の時点でインデックスを取得できますが、これはiterators では不可能です。
- では
iterator、利用可能な次の要素のみをチェックできますが、listiterator前と次の要素をチェックできます。
- では
listiteratorもし、時間の任意の時点でトラバースしながら、新しい要素を追加することができます。では不可能iteratorです。
- では
listiterator、トラバース中に要素を変更できますが、では不可能iteratorです。
イテレータのルックアンドフィール:
public interface Iterator<E> {
boolean hasNext();
E next();
void remove(); //optional-->use only once with next(),
dont use it when u use for:each
}
ListIteratorのルックアンドフィール:
public interface ListIterator<E> extends Iterator<E> {
boolean hasNext();
E next();
boolean hasPrevious();
E previous();
int nextIndex();
int previousIndex();
void remove(); //optional
void set(E e); //optional
void add(E e); //optional
}