Need for Iterators 7 2 T getint at

  • Slides: 13
Download presentation
Need for Iterators 7 2 T get(int at) 6 1 for (int i=0; i<

Need for Iterators 7 2 T get(int at) 6 1 for (int i=0; i< L. size(); i++) {. . L. get(i). . . } 8 We cannot improve this, because the internal information and structures are protected. But, 1+2+3+. . . +n = (1+n)*n/2 O(n 2) 9/9/2020 3 5 this is a common application Suppose L. size() = n O(n) ? 4 IT 179 1

Iterable<T> {interface} Iterable + Iterator<T> iterator(); My. List<T> {interface} (see the java document) ….

Iterable<T> {interface} Iterable + Iterator<T> iterator(); My. List<T> {interface} (see the java document) …. package my. Util; import java. util. Iterator; import java. util. No. Such. Element. Exception; public class My. SLinked. List <T> implements My. List<T>{ {. . . . public Iterator<T> iterator() { return new my. Iterator(); }. . . } 9/9/2020 Another inner class that defines our iterator. IT 179 2

Iterator<T> {interface} API java. util. Iterator<E> head 0 + boolean has. Next(); + T

Iterator<T> {interface} API java. util. Iterator<E> head 0 + boolean has. Next(); + T next(); + void remove(); 1 2 3 next. Node 4 5 6 Point to the node that will be returned by next call of next() seen. Node Point to the node that is seen (may be removed later) prev. Node 9/9/2020 Point to the node before the seen node. IT 179 3

my. Iterator inner class package my. Util; import java. util. Iterator; import java. util.

my. Iterator inner class package my. Util; import java. util. Iterator; import java. util. No. Such. Element. Exception; public class My. SLinked. List<T> implements My. List<T>, Iterable<T>{. . /***** This is an inner class for my. Iterator ******/ public class my. Iterator implements Iterator<T> { private Node<T> prev. Node=null, seen. Node=null, next. Node=head; // current node to be seen //by next() public boolean has. Next() { return (next. Node != null); } … … } /***** This is the end of inner class my. Iterator ******/ 9/9/2020 IT 179 4

my. Iterator (countined) /*****This is an inner class for my. Iterator ******/ public class

my. Iterator (countined) /*****This is an inner class for my. Iterator ******/ public class my. Iterator implements Iterator<T> {. . . . public T next() { if (! has. Next()) throw new No. Such. Element. Exception("No. Such. Element. Exception: "); if (seen. Node != null) // The seen node is not removed prev. Node = seen. Node; seen. Node = next. Node; next. Node = next. Node. next; return seen. Node. data; }. . . . /***** This is the end of inner class my. Iterator ******/ 9/9/2020 IT 179 5

my. Iterator (countined) /*****This is an inner class for my. Iterator ******/ public class

my. Iterator (countined) /*****This is an inner class for my. Iterator ******/ public class my. Iterator implements Iterator<T> {. . . . public void remove() { if (seen. Node == null) throw new Illegal. State. Exception("Illegal. State. Exception: "); if (seen. Node == head) { head = seen. Node. next; } else { prev. Node. next = next. Node; } seen. Node = null; // it's gone size--; }. . . . } /***** This is the end of inner class my. Iterator ******/ 9/9/2020 IT 179 6

public Iterator<T> iterator() { return new my. Iterator(); } internal class Anonymous Class public

public Iterator<T> iterator() { return new my. Iterator(); } internal class Anonymous Class public Iterator<T> iterator() { return new Iterator<T>() { /* Return an Anonymous class as an iterator ***/ …… /* same as the body the my. Iterator */ …… /* This is the end of the Anonymous class *****/ }; /** don't forget "; " **/ } /* end of iterator */ 9/9/2020 IT 179 7

A Marker Interface static <T extends Random. Access> void swap(T x, int i, int

A Marker Interface static <T extends Random. Access> void swap(T x, int i, int j) { if (!(x instanceof List)) return; T temp = x. get(i); x. set(i, x. get(j)); x. set(j, temp); } public static void main(String[] args) { Array. List<Object> A = new Array. List<Object>(); Linked. List<Object> B = new Linked. List<Object>(); List<Object> C; . . . swap(A, 30, 95); swap(B, 30, 95); C = B swap(C, 30, 95); } 9/9/2020 IT 179 8

Single Linked List It is very common to add new items to the end

Single Linked List It is very common to add new items to the end of the list public void add(T item) { // append item to the end of the list add(size, item); } Easy Solution: tail head 52 Can Iterator help? No! 12 14 16 23 for (int i=0; i<n; i++) { something =. . . list. add(something); } 9/9/2020 45 1+2+3+. . . +n = (1+n)*n/2 O(n 2) Add to the tail (append) IT 179 48 9

API java. util. List. Iterator<T> Tail doesn’t help tail head 0 1 2 3

API java. util. List. Iterator<T> Tail doesn’t help tail head 0 1 2 3 4 next. Node List. Iterator<T> {interface} + + + + + boolean has. Next(); boolean has. Previous(); T next(); T previous(); void remove(); int next. Index() int previous. Index(); void add(T item); void set(T item); Point to the node that will be returned by next call of next() seen. Node Point to the node that is seen (may be removed later) prev. Node 9/9/2020 Point to the node before the seen node. IT 179 10

Single Linked List It is very common to add new items to the end

Single Linked List It is very common to add new items to the end of the list public void add(T item) { // append item to the end of the list add(size, item); } Easy Solution: tail head 52 Can Iterator help? No! 12 14 16 23 for (int i=0; i<n; i++) { something =. . . list. add(something); } 9/9/2020 45 1+2+3+. . . +n = (1+n)*n/2 O(n 2) Add to the tail (append) IT 179 48 11

API java. util. List. Iterator<T> Tail doesn’t help tail head 0 1 2 3

API java. util. List. Iterator<T> Tail doesn’t help tail head 0 1 2 3 4 next. Node List. Iterator<T> {interface} + + + + + boolean has. Next(); boolean has. Previous(); T next(); T previous(); void remove(); int next. Index() int previous. Index(); void add(T item); void set(T item); Point to the node that will be returned by next call of next() seen. Node Point to the node that is seen (may be removed later) prev. Node 9/9/2020 Point to the node before the seen node. IT 179 12

API java. util. List. Iterator<E> head tail 0 1 2 3 4 next. Node

API java. util. List. Iterator<E> head tail 0 1 2 3 4 next. Node List. Iterator<T> {interface} + + + + + boolean has. Next(); boolean has. Previous(); T next(); T previous(); void remove(); int next. Index() int previous. Index(); void add(T item); void set(T item); Point to the node that will be returned by next call of next() seen. Node Point to the node that is seen (may be removed later) 9/9/2020 IT 179 13