Download presentation
Presentation is loading. Please wait.
Published byJeffrey Allen Modified over 9 years ago
1
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com
2
CS-2852 Data Structures, Andrew J. Wozniewicz Agenda JCF iterator interfaces – Iterator – ListIterator JCF Iterable interfaces – Iterable – Collection – List JCF Iterable Collections – ArrayList – LinkedList Introduction to Testing
3
CS-2852 Data Structures, Andrew J. Wozniewicz Iterators An object Traversing a collection Hide implementation In computer science, an iterator is an object that allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation. Wikipedia Definition
4
CS-2852 Data Structures, Andrew J. Wozniewicz The Iterator Interface import java.util.* interface Iterator { boolean hasNext(); E next(); void remove(); // optional }
5
CS-2852 Data Structures, Andrew J. Wozniewicz Doubly-Linked List – getIterator() public class DoublyLinkedListIterable { public Iterator getIterator() { return new DoublyLinkedListIterator (this); }
6
CS-2852 Data Structures, Andrew J. Wozniewicz Doubly-Linked List – Iterator public class DoublyLinkedListIterator implements Iterator { DoublyLinkedListIterable theList; Node current; public DoublyLinkedListIterator( DoublyLinkedListIterable list) { theList = list; reset(); } //TODO operations } DEMO
7
CS-2852 Data Structures, Andrew J. Wozniewicz The ListIterator Interface import java.util.* public interface ListIterator extends Iterator { public boolean hasPrevious(); public E previous(); public int nextIndex(); public int previousIndex(); public void set(E o); //optional public void add(E o); //optional }
8
CS-2852 Data Structures, Andrew J. Wozniewicz The ListIterator Interface Expanded import java.util.* public interface ListIterator { boolean hasNext(); E next(); void remove(); // optional public boolean hasPrevious(); public E previous(); public int nextIndex(); public int previousIndex(); public void set(E o); //optional public void add(E o); //optional }
9
CS-2852 Data Structures, Andrew J. Wozniewicz Using Iterators I import java.util.* class IteratorDemo { public static void main(String args[]) { }
10
CS-2852 Data Structures, Andrew J. Wozniewicz Using Iterators II // create an array list ArrayList al = new ArrayList (); // add elements to the array list al.add("Charlie"); al.add("Alpha"); al.add("Echo"); al.add("Bravo"); al.add("Delta"); al.add("Foxtrot"); inside main(String args[])
11
CS-2852 Data Structures, Andrew J. Wozniewicz The Iterable Interface public interface Iterable { Iterator iterator(); } public interface Collection extends Iterable {... } public interface List extends Collection {... } The Basis for Collection, List Java Collections Framework
12
CS-2852 Data Structures, Andrew J. Wozniewicz Using Iterators III // display contents of al System.out.print(“Content of al: "); Iterator itr = al.iterator(); while (itr.hasNext()) { Object element = itr.next(); System.out.print(element + " "); } inside main(String args[])
13
CS-2852 Data Structures, Andrew J. Wozniewicz Using Iterators IV // modify objects being iterated ListIterator litr = al.listIterator(); While (litr.hasNext()) { String element = litr.next(); litr.set(element + "+"); } inside main(String args[])
14
CS-2852 Data Structures, Andrew J. Wozniewicz The List Interface public interface Iterable { Iterator iterator(); } public interface Collection extends Iterable {... } public interface List extends Collection { ListIterator listIterator(); ListIterator listIterator(int index);... } The Basis for Collection, List Java Collections Framework
15
CS-2852 Data Structures, Andrew J. Wozniewicz ArrayList Import java.util.* List al = new ArrayList (); al.add("Charlie"); al.remove(0); Resizable-array implementation of the List interface. As elements are added to an ArrayList, its capacity grows automatically. Implements all optional list operations. Permits null. Provides methods to manipulate size of the underlying data array. ( stack, queue, or double-ended queue). Java Collections Framework
16
CS-2852 Data Structures, Andrew J. Wozniewicz LinkedList import java.util.* List intList = new LinkedList (); intList.addLast(42); intList.addFirst(17); Doubly-linked list implementation of the List interface. Implements all optional list operations. Permits null. Provides uniformly named methods to get, remove and insert an element at the beginning and end of the list ( stack, queue, or double-ended queue). Java Collections Framework
17
CS-2852 Data Structures, Andrew J. Wozniewicz Summary JCF iterator interfaces – Iterator – ListIterator JCF iterable interfaces – Iterable – Collection – List JCF iterable Collections – ArrayList – LinkedList
18
Questions? Image copyright © 2010 andyjphoto.com
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.