1 / 27

Iterators

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

cianna
Download Presentation

Iterators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iterators An iterator permits you to examine the elements of a data structure one at a time.

  2. Iterator Methods Iterator ix = x.iterator(); constructs and initializes an iterator to examine the elements of x; constructed iterator is assigned to ix you must define the method iterator in the class for x

  3. Iterator Methods ix.hasNext() returns true iff x has a next element ix.next() throws NoSuchElementException if there is no next element returns next element otherwise

  4. Optional Iterator Method ix.remove() removes last element returned by ix.next() throws UnsupportedMethodException if method not implemented throws IllegalStateException if ix.next() not yet called or did not return an element

  5. Using An Iterator Iterator ix = x.iterator(); while (ix.hasNext()) examine(ix.next()); • vs • for (int i = 0; i < x.size(); i++) • examine(x.get(i));

  6. Merits Of An Iterator it is often possible to implement the method next so that its complexity is less than that of get many data structures do not have a get by index method iterators provide a uniform way to sequence through the elements of a data structure

  7. Java’s Array Linear List Class java.util.ArrayList Cadillac version of our ArrayLinearListWithIterator

  8. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next

  9. a b c d e c a e d b Memory Layout Layout of L = (a,b,c,d,e) using an array representation. A linked representation uses an arbitrary layout.

  10. firstNode Linked Representation c a e d b pointer (or link) in e is null use a variable firstNode to get to the first element a

  11. firstNode null a b c d e Normal Way To Draw A Linked List link or pointer field of node data field of node

  12. Chain firstNode null a b c d e A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a null pointer.

  13. next element Node Representation package dataStructures; class ChainNode { // package visible data members Object element; ChainNode next; // constructors come here }

  14. null null null element next element Constructors Of ChainNode ChainNode(Object element) {this.element = element;} ChainNode() {} ChainNode(Object element, ChainNode next) {this.element = element; this.next = next;}

  15. firstNode null a b c d e get(0) checkIndex(0); desiredNode = firstNode; // gets you to first node return desiredNode.element;

  16. firstNode null a b c d e get(1) checkIndex(1); desiredNode = firstNode.next; // gets you to second node return desiredNode.element;

  17. firstNode null a b c d e get(2) checkIndex(2); desiredNode = firstNode.next.next; // gets you to third node return desiredNode.element;

  18. firstNode null a b c d e get(5) checkIndex(5); // throws exception desiredNode = firstNode.next.next.next.next.next; // desiredNode = null return desiredNode.element; // null.element

  19. firstNode null a b c d e NullPointerException desiredNode = firstNode.next.next.next.next.next.next; // gets the computer mad // you get a NullPointerException

  20. firstNode null a b c d e Remove An Element remove(0) firstNode = firstNode.next;

  21. firstNode b beforeNode null a b d e c c c remove(2) first get to node just before node to be removed beforeNode = firstNode.next;

  22. remove(2) firstNode null a b c d e now change pointer in beforeNode beforeNode beforeNode.next = beforeNode.next.next;

  23. firstNode null f a b c d e newNode add(0,’f’) Step 1: get a node, set its data and link fields • ChainNode newNode = • new ChainNode(new Character(‘f’), firstNode);

  24. firstNode null f a b c d e newNode add(0,’f’) Step 2: update firstNode firstNode = newNode;

  25. firstNode null f a b c d e newNode One-Step add(0,’f’) firstNode = new ChainNode( new Character(‘f’), firstNode);

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

  27. newNode f firstNode null a b c d e beforeNode c Two-Step add(3,’f’) • beforeNode = firstNode.next.next; • beforeNode.next = new ChainNode(new Character(‘f’), • beforeNode.next);

More Related