Lists Contents • The List ADT • Test Yourself #1 • Java Interfaces • Lists vs. Arrays • Test Yourself #2 • The Java API and Lists • Implementing the List ADT • Implementing the add methods • Test Yourself #3 • Implementing the constructor • Iterators • What are iterators? • Implementing iterators • Testing The List ADT Our first ADT is the List: an ordered collection of items of some element type E. Note that this doesn't mean that the objects are in sorted order, it just means that each object has a position in the List, starting with position zero. Recall that when we think about an ADT, we think about both the external and internal views. The external view includes the "conceptual picture" and the set of "conceptual operations". The conceptual picture of a List is something like this: item 0 item 1 item 2 . . . item n and one reasonable set of operations is: Operation Description void add(E item) add item to the end of the List void add(int pos, E item) add item at position pos in the List, moving the
items originally in positions pos through size()-1 one place to the right to make room (error if pos is less than 0 or greater than size()) boolean contains(E item) return true iff item is in the List (i.e., there is an item x in the List such that x.equals(item )) int size() return the number of items in the List boolean isEmpty() return true iff the List is empty E get(int pos) return the item at position pos in the List (error if pos is less than 0 or greater than or equal to size()) E remove(int pos) remove and return the item at position pos in the List, moving the items originally in positions pos+1 through size()-1 one place to the
TEST YOURSELF #1 Question 1: What other operations on Lists might be useful? Define them by writing descriptions like those in thetable above. Question 2: Note that the second add method (the one that adds an item at a given position) can be called with a position that is equal to size(), but for the get and remove methods, the position has to be lessthan size(). Why?Question 3: Another useful abstract data type is called a Map. A Map stores unique "key" values with associated information. For example, you can think of a dictionary as a Map, where the keys are the words, and the associated information is the definitions.What are some other examples of Maps that you use?What are the useful operations on Maps? Define them by writing descriptions like those in the table above.Java InterfacesThe List ADT operations given in the table above describe the public interface of the List ADT, that is,
You've reached the end of your free preview.
Want to read all 13 pages?
- Spring '08
- Data Structures, Array, item