Lists - Lists Page 1 of 12 LISTS Contents The List ADT Test...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
L ISTS Contents z The List ADT { Test Yourself #1 z Java Lists { Test Yourself #2 { Implementing Our Own List Class ± Implementing the add methods ± Test Yourself #3 ± Implementing the constructor { Iterators ± What are they? ± How to implement them? z 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: and one reasonable set of operations is the following, where E is the type of an element in the list. item 0 item 1 item 2 . . . item n OPERATION DESCRIPTION void add(E ob) add ob to the end of the List void add(int pos, E ob) add ob 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() ) return true iff ob is in the List (i.e., there is an item x in the List such that Page 1 of 12 Lists 2008/3/27
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Many other operations are possible; when designing an ADT, you should try to provide enough operations to make the ADT useful in many contexts, but not so many that it gets confusing. It is not always easy to achieve this goal; it will sometimes be necessary to add operations in order for a new application to use an existing ADT. TEST YOURSELF #1 Question 1. What other operations on Lists might be useful? Define them by writing descriptions like those in the table 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 less than 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 Lists The Java.util package provides a List interface (with many more methods than the ones listed above), and a number of classes that implement that interface, including two that we will discuss in some detail: the ArrayList class and the LinkedList class. In some ways, a Java
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 12

Lists - Lists Page 1 of 12 LISTS Contents The List ADT Test...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online