250 likes | 414 Views
CSC 332 – Algorithms and Data Structures. Collections API. Dr. Paige H. Meeker Computer Science Presbyterian College, Clinton, SC. Announcements. Midterm to move to 10/12 Speaker to speak 10/10 – you need to be there! New programming assignment due 10/4. Data Structures.
E N D
CSC 332 – Algorithms and Data Structures Collections API Dr. Paige H. Meeker Computer Science Presbyterian College, Clinton, SC
Announcements • Midterm to move to 10/12 • Speaker to speak 10/10 – you need to be there! • New programming assignment due 10/4
Data Structures • Proper representation of data helps achieve efficiency • Representations and allowable operations on data are called its “data structure” • Data structures typically allow insertions, but restrict data access and deletions based on the type of structure
Collections API • Supporting Java library • Provides collection of commonly used data structures • Also provides some commonly used generic algorithms • Uses inheritance • Most reside in java.util
Goals • Provide a description (in general terms) of examples and applications of data structures • Describe the basics of the Collections API • Peek ahead into chapter 16 at some implementations
Data Structures • Representation of data and the operations allowed on that data • Allow component reuse • Usually (but not always) store a collection of objects and provide methods to add, remove, or access those objects in specific ways.
Generic data structure protocol Most data structures follow the protocol of figure 6.1 of your text Let’s view it now… (open your book to page 203)
Iterators • Iterators are objects used to move through a collection. In the loop: for (int i = 0; i < v.length; i++) System.out.println(v[i]); i is the iterator because it is the object used to control the iteration. We want a more generic iterator, though.
Iterators We want the code that performs access to the container to be independent of the type of container – so the code would be replaced by something like… for(itr = v.first(); itr.isValid(); itr.advance()) System.out.println(itr.getData()); This code assumes methods of first(), isValid(), advance(), and getData() – useful methods for this purpose.
Basic Iterator Design • Two methods: • hasNext() – returns true if the iteration has not yet been exhausted • next() – returns the next item in the collection and advances the current position. • See figures 6.2 and 6.3, p. 205 • Limitations? Can’t go backwards
Inheritance Based Iterator Design • We have abstracted the idea of iteration into an iterator class; now, we need to define an iterator interface to give us even more freedom of coding design. This allows clients to program to the interface
Inheritance Based Iterator Design • (See figures 6.5-6.8, p. 207-208) • The iterator() creates and returns an “Iterator” object whose actual type is unknown (we are using the interface type). This is known as a “factory method” • Now, code does not have to reference a specific type of iterator but can use the generic Iterator object provided by the interface.
Collections Interface The collections interface represent a group of objects known as its elements. All containers within this interface support the following operations:
Collections Interface boolean isEmpty() int size() boolean add(AnyType x) boolean contains(Object x) boolean remove(Object x) void clear() Object [] toArray() <OtherType> OtherType [] toArray(OtherType [] arr) Java.util.Iterator<AnyType> iterator()
Collection Interface • See interface here
Iterator Interface • Contains: boolean hasNext() AnyType next() void remove() View here. • Each collection defines its own implementation of the iterator interface.
Now… to print any collection… /** * Print any collection using itr directly */ public static <AnyType> void printCollection( Collection<AnyType> c ) { Iterator<AnyType> itr = c.iterator(); while( itr.hasNext() ) System.out.print( itr.next() + " " ); System.out.println( ); }
Now… to print any collection… /** * Print any collection using enhanced for loop */ public static <AnyType> void printCollection( Collection<AnyType> c ) { for( AnyType val : c ) System.out.print( val + " " ); System.out.println( ); }
List Interface • List – a collection of items in which the items have position • Example: an array • Extends the Collection interface and adds three methods: • AnyType get(int idx) • AnyType set(int idx) • ListIterator<AnyType> listIterator(int pos) • Looks like…
ListIterator The ListIterator interface is just like an Iterator except it is bidirectional – we can both advance and retreat!
ListIterator package weiss.util; /** * ListIterator interface for List interface. */ public interface ListIterator<AnyType> extends Iterator<AnyType> { /** * Tests if there are more items in the collection when iterating in reverse. * @return true if there are more items in the collection when traversing in reverse. */ boolean hasPrevious( ); /** * Obtains the previous item in the collection. * @return the previous (as yet unseen) item in the collection when traversing in reverse. */ AnyType previous( ); /** * Remove the last item returned by next or previous. Can only be called once after next or previous. */ void remove( ); }
LinkedList class • Two implementations of the List Interface – ArrayList and LinkedList • Vector (a depreciated class) also implements LinkedList • LinkedLists store objects in a node that contains the object and a reference to the next node in the list (See fig. 6.19 p. 223)
LinkedList Supports: void addLast(AnyType element) void addFirst(AnyType element) AnyType getFirst() // returns 1st element AnyType element() // returns 1st element AnyType getLast() AnyType removeFirst() //removes 1st element AnyType remove() //removes 1st element AnyType removeLast()
Stacks and Queues • See Class Handout
Homework • See program assignment 4 (So much to do… so little time) It’s due Tuesday (10/4)