Data Structures and Abstractions with Java 5 th
Data Structures and Abstractions with Java™ 5 th Edition Chapter 13 Iterators for the ADT List Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterators • An iterator – An object that enables you to traverse entries in a data collection • Possible way to provide an ADT with traversal operations – Define them as ADT operations • Better way – Implement the iterator methods within their own class Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Separate Class Iterator Figure 13 -1 A separate class iterator with a reference to an ADT, an indicator of its position within the iteration, and no knowledge of the ADT’s implementation Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Separate Class Iterator FIGURE 13 -2 Changes to a list and next. Position when removing Chris from the list Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Separate Class Iterator /** A class that represents an iterator for the ADT list. */ public class Separate. Iterator<T> implements Iterator<T> { private List. Interface<T> list; private int next. Position; // Position of entry last returned by next() private boolean was. Next. Called; // Needed by remove public Separate. Iterator(List. Interface<T> my. List) { list = my. List; next. Position = 0; was. Next. Called = false; } // end constructor /* < Implementations of the methods has. Next, next, and remove go here. >. . . */ } // end Separate. Iterator LISTING 13 -1 An outline of the class Separate. Iterator Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Separate Class Iterator public boolean has. Next() { return next. Position < list. get. Length(); } // end has. Next Implementation of has. Next Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Separate Class Iterator public T next() { if (has. Next()) { was. Next. Called = true; next. Position++; return list. get. Entry(next. Position); } else throw new No. Such. Element. Exception("Illegal call to next(); " + "iterator is after end of list. "); } // end next Implementation of next Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Separate Class Iterator public void remove() { if (was. Next. Called) { // next. Position was incremented by the call to next(), so // it is the position number of the entry to be removed list. remove(next. Position); next. Position--; // A subsequent call to next() must be // unaffected by this removal was. Next. Called = false; // Reset flag } else throw new Illegal. State. Exception("Illegal call to remove(); " + "next() was not called. "); } // end remove Implementation of remove Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Inner Class Iterator FIGURE 13 -3 An inner class iterator with direct access to the linked chain that implements the ADT Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Inner Class Iterator /** An interface for the ADT list that has an iterator. */ public interface List. With. Iterator. Interface<T> extends List. Interface<T>, Iterable<T> { public Iterator<T> get. Iterator(); } // end List. With. Iterator. Interface LISTING 13 -2 The interface List. With. Iterator. Interface Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Inner Class Iterator (Part 1) /** A class that implements the ADT list by using a chain of linked nodes. The list has an iterator. The class is similar to LList. */ public class Linked. List. With. Iterator<T> implements List. With. Iterator. Interface<T> { private Node first. Node; private int number. Of. Entries; ; public Linked. List. With. Iterator() { initialize. Data. Fields(); } // end default constructor /* < Implementations of the methods of the ADT list go here; you can see them in Chapter 12, beginning at Segment 12. 7 >. . . */ public Iterator<T> iterator() { return new Iterator. For. Linked. List(); } // end iterator public Iterator<T> get. Iterator() { return iterator(); } // end get. Iterator LISTING 13 -3 An outline of the class Linked. List. With. Iterator Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Inner Class Iterator (Part 2) private class Iterator. For. Linked. List implements Iterator<T> { private Node next. Node; private Iterator. For. Linked. List() { next. Node = first. Node; } // end default constructor // Implementations of the methods in the interface Iterator go here. } // end Iterator. For. Linked. List private class Node { // Implementations of the methods in inner class Node go here. } // end Node } // end Linked. List. With. Iterator LISTING 13 -3 An outline of the class Linked. List. With. Iterator Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Inner Class Iterator public T next() { T result; if (has. Next()) { result = next. Node. get. Data(); next. Node = next. Node. get. Next. Node(); // Advance iterator } else throw new No. Such. Element. Exception("Illegal call to next(); " + "iterator is after end of list. "); return result; // Return next entry in iteration } // end next The method next. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Inner Class Iterator public boolean has. Next() { return next. Node != null; } // end has. Next The method has. Next Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Inner Class Iterator public void remove() { throw new Unsupported. Operation. Exception("remove() is not supported " + "by this iterator"); } // end remove The method remove. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterators for Array-Based Lists FIGURE 13 -4 Changes to the array of list entries and next. Index when removing Chris from the list Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterators for Array-Based Lists (Part 1) /** A class that implements the ADT list by using a resizable array and gives it an iterator. */ public class Array. List. With. Iterator<T> implements List. With. Iterator. Interface<T> { private T[] list; // Array of list entries; ignore list[0] private int number. Of. Entries; private boolean integrity. OK = false; private static final int DEFAULT_CAPACITY = 25; private static final int MAX_CAPACITY = 10000; public Array. List. With. Iterator() { this(DEFAULT_CAPACITY); } // end default constructor LISTING 13 -4 An outline of the class Array. List. With. Iterator Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterators for Array-Based Lists (Part 2) public Array. List. With. Iterator(int initial. Capacity) { integrity. OK = false; // Is initial. Capacity too small? if (initial. Capacity < DEFAULT_CAPACITY) initial. Capacity = DEFAULT_CAPACITY; else // Is initial. Capacity too big? check. Capacity(initial. Capacity); // The cast is safe because the new array contains null entries @Suppress. Warnings("unchecked") T[] temp. List = (T[])new Object[initial. Capacity + 1]; list = temp. List; number. Of. Entries = 0; integrity. OK = true; } // end constructor /* < Implementations of the methods of the ADT list go here; you can see them in Chapter 11, beginning at Segment 11. 5. */ LISTING 13 -4 An outline of the class Array. List. With. Iterator Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterators for Array-Based Lists (Part 3) public Iterator<T> iterator() { return new Iterator. For. Array. List(); } // end iterator public Iterator<T> get. Iterator() { return iterator(); } // end get. Iterator private class Iterator. For. Array. List implements Iterator<T> { private int next. Index; // Index of next entry in the iteration private boolean was. Next. Called; // Needed by remove private Iterator. For. Array. List() { next. Index = 1; // Iteration begins at list's first entry was. Next. Called = false; } // end default constructor // Implementations of the methods in the interface Iterator go here. } // end Iterator. For. Array. List } // end Array. List. With. Iterator LISTING 13 -4 An outline of the class Array. List. With. Iterator Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterators for Array-Based Lists public boolean has. Next() { return next. Index <= number. Of. Entries; } // end has. Next Method has. Next Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterators for Array-Based Lists public T next() { check. Integrity(); if (has. Next()) { was. Next. Called = true; T next. Entry = list[next. Index]; next. Index++; // Advance iterator return next. Entry; } else throw new No. Such. Element. Exception("Illegal call to next(); " + "iterator is after end of list. "); } // end next Method next Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Iterators for Array-Based Lists public void remove() { check. Integrity(); if (was. Next. Called) { // next. Index was incremented by the call to next, so it is // 1 larger than the position number of the entry to be removed Array. List. With. Iterator. this. remove(next. Index − 1); next. Index−−; // Index of next entry in iteration was. Next. Called = false; // Reset flag } else throw new Illegal. State. Exception("Illegal call to remove(); " + "next() was not called. "); } // end remove Implementation of remove Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Why Are Iterator Methods in Their Own Class? (Part 1) /** A linked implementation of the ADT list that includes iterator operations as ADT operations. */ public class List. With. Traversal<T> implements List. Interface<T>, Iterator<T> { private Node first. Node; private int number. Of. Entries; private Node next. Node; // Node containing next entry in iteration public List. With. Traversal() { initialize. Data. Fields(); } // end default constructor /* < Implementations of the remaining methods of the ADT list go here. */ // Initializes the class's data fields to indicate an empty list. private void initialize. Data. Fields() { first. Node = null; number. Of. Entries = 0; next. Node = null; } // end initialize. Data. Fields LISTING 13 -5 An outline of the class List. With. Traversal Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Why Are Iterator Methods in Their Own Class? (Part 2) // Methods in the interface Iterator go here /** Sets the traversal to the beginning of the list. This method is NOT in the interface Iterator. */ public void reset. Traversal() { next. Node = first. Node; } // end reset. Traversal private class Node { // Methods in the inner Node class go here: } // end Node } // end List. With. Traversal LISTING 13 -5 An outline of the class List. With. Traversal Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Why Are Iterator Methods in Their Own Class? • These traversal methods can execute quickly – They have direct access to the underlying data structure • Disadvantages – Only one traversal at a time – Operation such as reset. Traversal necessary – “interface bloat” Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Array-Based Implementation of the Interface List. Iterator /** An interface for the ADT list that has an iterator implementing the interface List. Iterator. */ public interface List. With. List. Iterator. Interface<T> extends Iterable<T>, List. Interface<T> { public List. Iterator<T> get. Iterator(); } // end List. With. List. Iterator. Interface LISTING 13 -6 The interface List. With. List. Iterator. Interface Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Array-Based Implementation of the Interface List. Iterator (Part 1) /** A class that implements the ADT list by using an array. The list has entries that are numbered beginning at 1. The list has an iterator that implements the interface List. Iterator positions (indexes) are numbered beginning at 0. */ public class Array. List. With. List. Iterator<T> implements List. With. List. Iterator. Interface<T> { private T[] list; // Array of list entries; ignore list[0] private int number. Of. Entries; private boolean integrity. OK; private static final int DEFAULT_CAPACITY = 25; private static final int MAX_CAPACITY = 10000; public Array. List. With. List. Iterator() { this(DEFAULT_CAPACITY); } // end default constructor LISTING 13 -7 An outline of the class Array. List. With. List. Iterator Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Array-Based Implementation of the Interface List. Iterator (Part 2) public Array. List. With. List. Iterator(int initial. Capacity) { integrity. OK = false; // Is initial. Capacity too small? if (initial. Capacity < DEFAULT_CAPACITY) initial. Capacity = DEFAULT_CAPACITY; else // Is initial. Capacity too big? check. Capacity(initial. Capacity); // The cast is safe because the new array contains null entries @Suppress. Warnings("unchecked") T[] temp. List = (T[])new Object[initial. Capacity + 1]; list = temp. List; number. Of. Entries = 0; integrity. OK = true; } // end constructor /* < Implementations of the methods of the ADT list go here; */ LISTING 13 -7 An outline of the class Array. List. With. List. Iterator Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Array-Based Implementation of the Interface List. Iterator (Part 3) public List. Iterator<T> get. Iterator() { return new List. Iterator. For. Array. List(); } // end get. Iterator public Iterator<T> iterator() { return get. Iterator(); } // end iterator private class List. Iterator. For. Array. List implements List. Iterator<T> { // The details of this class begin with Segment 13. 24. } // end List. Iterator. For. Array. List } // end Array. List. With. List. Iterator LISTING 13 -7 An outline of the class Array. List. With. List. Iterator Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Inner Class Iterator for Array-Based Lists (Part 1) FIGURE 13 -5 Possible contexts in which the method remove of the iterator traversal throws an exception when called Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Inner Class Iterator for Array-Based Lists (Part 2) FIGURE 13 -5 Possible contexts in which the method remove of the iterator traversal throws an exception when called Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Inner Class private enum Move {NEXT, PREVIOUS} private class List. Iterator. For. Array. List implements List. Iterator<T> { private int next. Index; // Index of next entry in the iteration private boolean is. Remove. Or. Set. Legal; private Move last. Move; private List. Iterator. For. Array. List() { next. Index = 1; // Iteration begins at list's first entry is. Remove. Or. Set. Legal = false; last. Move = null; } // end default constructor Beginning of the inner class. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Inner Class public boolean has. Next() { return next. Index <= number. Of. Entries; } // end has. Next Method has. Next. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Inner Class public T next() { if (has. Next()) { last. Move = Move. NEXT; is. Remove. Or. Set. Legal = true; T next. Entry = list[next. Index]; next. Index++; // Advance iterator return next. Entry; } else throw new No. Such. Element. Exception("Illegal call to next(); " + "iterator is after end of list. "); } // end next Implementation of next. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Inner Class public boolean has. Previous() { return (next. Index > 1) && (next. Index <= number. Of. Entries + 1); } // end has. Previous public T previous() { if (has. Previous()) { last. Move = Move. PREVIOUS; is. Remove. Or. Set. Legal = true; T previous. Entry = list[next. Index - 1]; next. Index--; // Move iterator back return previous. Entry; } else throw new No. Such. Element. Exception("Illegal call to previous(); " + "iterator is before beginning of list. "); } // end previous Methods has. Previous and previous. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Inner Class public int next. Index() { int result; if (has. Next()) result = next. Index - 1; // Change to zero-based numbering of iterator else result = number. Of. Entries; // End-of-list flag return result; } // end next. Index public int previous. Index() { int result; if (has. Previous()) result = next. Index - 2; // Change to zero-based numbering of iterator else result = -1; // Beginning-of-list flag return result; } // end previous. Index Methods next. Index and previous. Index Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Inner Class public void add(T new. Entry) { is. Remove. Or. Set. Legal = false; // Insert new. Entry immediately before the iterator's current position Array. List. With. List. Iterator. this. add(next. Index, new. Entry); next. Index++; } // end add The method add. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Inner Class FIGURE 13 -6 Changes to the array of list entries and next. Index when adding Ben to the list Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Inner Class FIGURE 13 -7 Changes to the array of list entries and next. Index when removing Chris from the list Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Inner Class public void remove() { if (is. Remove. Or. Set. Legal) { is. Remove. Or. Set. Legal = false; if (last. Move. equals(Move. NEXT)) { // next() called, but neither add() nor remove() has been called since. // Remove entry last returned by next(). // next. Index is 1 more than the index of the entry // returned by next() Array. List. With. List. Iterator. this. remove(next. Index - 1); next. Index--; // Move iterator back } else { // previous() called, but neither add() nor remove() has been called since // Remove entry last returned by previous(). // next. Index is the index of the entry returned by previous(). Array. List. With. List. Iterator. this. remove(next. Index); } // end if } else throw new Illegal. State. Exception("Illegal call to remove(); " + "next() or previous() not called, OR " + "add() or remove() called since then. "); } // end remove An implementation of remove. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
The Inner Class public void set(T new. Entry) { if (is. Remove. Or. Set. Legal) { if (last. Move. equals(Move. NEXT)) list[next. Index - 1] = new. Entry; // Replace entry last returned by next() else { // Assertion: last. Move. equals(Move. PREVIOUS) list[next. Index] = new. Entry; // Replace entry last returned by previous() } // end if } else throw new Illegal. State. Exception("Illegal call to set(); " + "next() or previous() not called, OR " + "add() or remove() called since then. "); } // end set An implementation of method set. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
End Chapter 13 Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
- Slides: 42