1 / 25

CSC 332 – Algorithms and Data Structures

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.

fawn
Download Presentation

CSC 332 – Algorithms and Data Structures

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. CSC 332 – Algorithms and Data Structures Collections API Dr. Paige H. Meeker Computer Science Presbyterian College, Clinton, SC

  2. Announcements • Midterm to move to 10/12 • Speaker to speak 10/10 – you need to be there! • New programming assignment due 10/4

  3. 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

  4. 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

  5. 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

  6. 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.

  7. 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)

  8. 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.

  9. 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.

  10. 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

  11. 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

  12. 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.

  13. Collections Interface The collections interface represent a group of objects known as its elements. All containers within this interface support the following operations:

  14. 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()

  15. Collection Interface • See interface here

  16. Iterator Interface • Contains: boolean hasNext() AnyType next() void remove() View here. • Each collection defines its own implementation of the iterator interface.

  17. 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( ); }

  18. 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( ); }

  19. 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…

  20. ListIterator The ListIterator interface is just like an Iterator except it is bidirectional – we can both advance and retreat!

  21. 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( ); }

  22. 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)

  23. 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()

  24. Stacks and Queues • See Class Handout

  25. Homework • See program assignment 4 (So much to do… so little time) It’s due Tuesday (10/4)

More Related