O'Reilly logo

Dart: Scalable Application Development by Ivo Balbaert, Sergey Akopkokhyants, Davy Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

BidirectionalIterator

Sometimes, we need to iterate over a collection of elements in both directions. To help in such cases, Dart provides BidirectionalIterator. In the following code, BiListIterator is the implementation of BidirectionalIterator:

class BiListIterator<E> implements BidirectionalIterator<E> {
  final Iterable<E> _iterable;
  final int _length;
  int _index;
  E _current;

The constructor has an extra optional back parameter that defines the direction of the iteration:

  BiListIterator(Iterable<E> iterable, {bool back:false}) :
    _iterable = iterable, _length = iterable.length,
    _index = back ? iterable.length - 1 : 0;

  E get current => _current;

The following code shows the moveNext method of the Iterator to move forward. This and the next method ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required