Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 209 Software Development Iterators.

Similar presentations


Presentation on theme: "Computer Science 209 Software Development Iterators."— Presentation transcript:

1 Computer Science 209 Software Development Iterators

2 Implementing Equals public boolean equals(Object other){ if (this == other) return true; if (! (other instanceof LinkedStack)) return false; LinkedStack otherStack = (LinkedStack )other; if (this.size() != otherStack.size()) return false; return this.containsAll(otherStack) && otherStack.containsAll(this); } Can result in quadratic running time What if the two stacks contain the same elements, but they are not in the same order?

3 Implementing Equals public boolean equals(Object other){ if (this == other) return true; if (! (other instanceof LinkedStack)) return false; LinkedStack otherStack = (LinkedStack )other; if (this.size() != otherStack.size()) return false; for (int i = 0; i < this.size(); i++) if (! this.list.get(i).equals(otherStack.list.get(i))) return false; return true; } get runs in constant time for ArrayList but in linear time for LinkedStack Results in quadratic running time for LinkedStack.equals

4 Implementing Equals public boolean equals(Object other){ if (this == other) return true; if (! (other instanceof LinkedStack)) return false; LinkedStack otherStack = (LinkedStack )other; if (this.size() != otherStack.size()) return false; Iterator otherIter = otherStack.iterator(); for (E thisElement : this) if (! thisElement.equals(otherIter.next())) return false; return true; } next runs in constant time for any collection’s iterator Results in linear running time for any equals

5 The Iterator Interface public interface Iterator { public boolean hasNext() public E next() public void remove() } remove deletes the object most recently accessed with next remove must be included in the implementing class, but need not be supported (can throw an UnsupportedOperationException )

6 Using an Iterator // add a bunch of objects to col Iterator iter = col.iterator(); Every collection class that supports an iterator must provide an iterator method. This method returns an instance of a class that implements the Iterator interface anIteratoraCollection A sequence of elements

7 // add a bunch of objects to col Iterator iter = col.iterator(); while (iter.hasNext()){ SomeType obj = iter.next(); System.out.println(obj); } Using an Iterator DDD collection iterator

8 // add a bunch of objects to col Iterator iter = col.iterator(); while (iter.hasNext()){ SomeType obj = iter.next(); System.out.println(obj); } Using an Iterator DDD collection iterator

9 // add a bunch of objects to col Iterator iter = col.iterator(); while (iter.hasNext()){ SomeType obj = iter.next(); System.out.println(obj); } Using an Iterator DDD collection iterator

10 // add a bunch of objects to col Iterator iter = col.iterator(); while (iter.hasNext()){ SomeType obj = iter.next(); System.out.println(obj); } Using an Iterator DDD collection iterator

11 // add a bunch of objects to col for (SomeType obj : col) System.out.println(obj); Iterable and the for -each Loop DDD collection iterator If col implements the Iterable interface, the client can use a for -each loop instead

12 Use in AbstractCollection abstract public class AbstractCollection implements Collection { public void clear(){ Iterator iter = this.iterator(); while (iter.hasNext()){ iter.next(); iter.remove(); } public boolean remove(Object o){ Iterator iter = this.iterator(); while (iter.hasNext()) if (iter.next().equals(o)){ iter.remove(); return true; } return false; }

13 public boolean hasNext() public E next() Preconditions on Methods hasNext has no preconditions next has two preconditions: hasNext returns true the underlying collection has not been modified by one of that collection’s mutators during the lifetime of that iterator

14 // Add a bunch of objects to col Iterator iter = col.iterator(); while (iter.hasNext()){ SomeType obj = iter.next(); } SomeType obj = iter.next(); // This should cause an exception Error: Run Out of Elements

15 // Add a bunch of objects to col Iterator iter = col.iterator(); while (iter.hasNext()){ col.removeLast(); SomeType obj = iter.next(); // This should cause an exception } Error: Inconsistent Data Using mutators in conjunction with iterators is a bad practice

16 An Iterator Implementation The iterator method is in the Iterable interface public interface TrueStack extends Collection { public E pop(); public void push(E newElement); public E peek(); } > Collection > Iterable > TrueStack

17 public class ArrayStack extends AbstractCollection implements TrueStack { private List list; // Code for constructors, push, pop, peek, size, and add public Iterator iterator(){ return list.iterator(); } An Iterator Implementation Problem: a list’s iterator supports the remove method

18 Another Design Strategy By using the list’s iterator, we expose it to the client (Law of Demeter?) Let’s use it, but wrap our own iterator object around it That allows us to control what’s supported and what’s not

19 public class ArrayStack extends AbstractCollection implements TrueStack { private List list; // Code for push, pop, peek, size, and add // Code for the iterator method // Code for the class that implements the Iterator // interface } An Iterator Implementation Define the iterator class as a private inner class.

20 public Iterator iterator(){ return new StackIterator (this.iterator()); } private class StackIterator implements Iterator { public boolean hasNext(){ return false; } public E next(){ return null; } public void remove(){ } The Implementing Class Nested within ArrayStack

21 public Iterator iterator(){ return new StackIterator (list.iterator()); } private class StackIterator implements Iterator { private Iterator iter; private StackIterator(Iterator iter){ this.iter = iter; } // Other methods } The Implementing Class Nested within ArrayStack

22 private class StackIterator implements Iterator { private Iterator iter; public boolean hasNext(){ return iter.hasNext(); } public E next(){ return iter.next(); } public void remove(){ throw new UnsupportedOperationException( "remove not supported by Stack"); } Other Methods


Download ppt "Computer Science 209 Software Development Iterators."

Similar presentations


Ads by Google