iterators iterator methods
play

Iterators Iterator Methods Iterator ix = x.iterator(); constructs - PDF document

Iterators Iterator Methods Iterator ix = x.iterator(); constructs and initializes an iterator to examine the elements of x; constructed iterator is assigned to ix An iterator permits you to examine the elements of a data structure one at a


  1. Iterators Iterator Methods Iterator ix = x.iterator(); constructs and initializes an iterator to examine the elements of x; constructed iterator is assigned to ix An iterator permits you to examine the elements of a data structure one at a time. you must define the method iterator in the class for x Iterator Methods Optional Iterator Method ix.hasNext() ix.remove() returns true iff x has a next element removes last element returned by ix.next() throws UnsupportedMethodException if ix.next() method not implemented throws NoSuchElementException if throws IllegalStateException if ix.next() there is no next element not yet called or did not return an returns next element otherwise element

  2. Using An Iterator Merits Of An Iterator • it is often possible to implement the Iterator ix = x.iterator(); method next so that its complexity while (ix.hasNext()) is less than that of get examine(ix.next()); • many data structures do not have a get by index method • iterators provide a uniform way to vs sequence through the elements of a data structure for (int i = 0; i < x.size(); i++) examine(x.get(i)); Java’s Array Linear List Class Linked Representation • list elements are stored, in memory, java.util.ArrayList in an arbitrary order Cadillac version of our • explicit information (called a link) ArrayLinearListWithIterator is used to go from one element to the next

  3. Linked Representation Memory Layout Layout of L = (a,b,c,d,e) using an array representation. c a e d b a b c d e firstNode pointer (or link) in e is null A linked representation uses an arbitrary layout. use a variable firstNode to get to the first element a c a e d b Normal Way To Draw A Linked List Chain firstNode firstNode null a b c d e null a b c d e •A chain is a linked list in which each node represents one element. link or pointer field of node • There is a link or pointer from one element to data field of node the next. • The last node has a null pointer.

  4. Node Representation Constructors Of ChainNode package dataStructures; null ChainNode() {} null class ChainNode { null // package visible data members ChainNode(Object element) element Object element; next {this.element = element;} ChainNode next; element // constructors come here ChainNode(Object element, ChainNode next) next } {this.element = element; element this.next = next;} get(0) get(1) firstNode firstNode null null a b c d e a b c d e checkIndex(0); checkIndex(1); desiredNode = firstNode; // gets you to first node desiredNode = firstNode.next; // gets you to second node return desiredNode.element; return desiredNode.element;

  5. get(2) get(5) firstNode firstNode null null a b c d e a b c d e checkIndex(2); checkIndex(5); // throws exception desiredNode = firstNode.next.next; // gets you to third node desiredNode = firstNode.next.next.next.next.next; return desiredNode.element; // desiredNode = null return desiredNode.element; // null.element NullPointerException Remove An Element firstNode firstNode null null a b c d e a b c d e desiredNode = remove(0) firstNode.next.next.next.next.next.next; // gets the computer mad firstNode = firstNode.next; // you get a NullPointerException

  6. remove(2) remove(2) firstNode firstNode null null cc b a b c d e a b c d e beforeNode beforeNode first get to node just before node to be removed now change pointer in beforeNode beforeNode = firstNode.next ; beforeNode.next = beforeNode.next.next; add(0,’f’) add(0,’f’) firstNode firstNode null null f a b c d e f a b c d e newNode newNode Step 1: get a node, set its data and link fields Step 2: update firstNode ChainNode newNode = firstNode = newNode; new ChainNode(new Character(‘f’), firstNode);

  7. add(3,’f’) One-Step add(0,’f’) firstNode newNode firstNode f null null a b c c d e f a b c d e newNode beforeNode • first find node whose index is 2 • next create a node and set its data and link fields firstNode = new ChainNode( ChainNode newNode = new ChainNode(new Character(‘f’), new Character(‘f’), firstNode); beforeNode.next); • finally link beforeNode to newNode beforeNode.next = newNode; Two-Step add(3,’f’) firstNode newNode f null a b c c d e beforeNode beforeNode = firstNode.next.next; beforeNode.next = new ChainNode(new Character(‘f’), beforeNode.next);

Recommend


More recommend