Download CS61B Lecture #7: Java Library Classes for Lists and Iterators and more Slides Data Structures and Algorithms in PDF only on Docsity!
CS61B Lecture
Reminder:
- Discussion section 114 (3-4 Th) moves to 3102 Etch., starting to- morrow.
Today:
- Java Library Classes for lists.
- Iterators, ListIterators
Abstracting “Listness”
- So far, we’ve seen fairly primitive types for representing lists of things.
- Arrays:
- Good: random access to items.
- Bad: hard to expand, insert items, or delete items.
- Linked lists (e.g., IntList):
- Good: easy to expand, insert, delete.
- Bad: must access in sequence, pointer manipulation can be tricky.
- Both used to represent same thing (sequence of things), but syntax for using very different,
- So hard to switch from one to the other if you change your mind.
Lists
- The list classes ArrayList and LinkedList both share many public methods, including: - size(), isEmpty(): Number of items, test for 0 items. - get(k): Get item #k, where 0 ≤ k < size(). - remove(k): Remove item #k. - clear(): Make the list empty. - set(k, x): Set item #k to x. - add(x), add(k,x): Add item to end, or a position k. - contains (x): True iff there is an item that equals x (according to
.equals method).
- indexOf (x): Gives the position ( 0 ≤ · <size()) of the first item
that .equals x, or -1 if there is none.
- Both expand as needed (automatically).
- A few methods specialized to one or the other (e.g. LinkedList.removeFirst().
Example: Read and interleave two lists
/** Read the sequence of words on INPUT, and print on
- OUTPUT in reverse order. */ static void readAndReverse (Scanner input, PrintStream output) { ArrayList L = new ArrayList (); while (input.hasNext ()) L.add (input.next ()); for (k = L.size ()-1; k >= 0; k -= 1) output.printf ("%s ", L.get (k)) }
- Not shown: import java.util.ArrayList; before class.
- Could also use a LinkedList. What problem might there be with that?
ListIterator
- Library also has type ListIterator
- These have both .previous() and .next() methods.
- Also allow insertion.
- Look at reversal again:
/** Read the sequence of words on INPUT, and print on
- OUTPUT in reverse order. */ static void readAndReverse (Scanner input, PrintStream output) { ArrayList L = new ArrayList (); ListIterator place = L.listIterator ();
while (input.hasNext ()) place.add (input.next ()); while (place.hasPrevious ()) System.out.printf ("%s ", place.previous ()); }
Primitive Types and Wrappers
- ArrayLists and the like can only take elements that are pointers, no
ints, doubles, booleans, etc.
- So, Java library contains corresponding wrapper classes: Integer,
Double, Boolean, etc.—all pointed-to objects
- So, new Integer(3) is 3. The intValue() method retrieves the 3.
- All very tedious, so Java 1.5 converts int ⇔ Integer automatically—
boxes 3 to make an Integer, unboxes to get 3 back.
- So we can do things like this:
ArrayList sqrts = new ArrayList(); while (inp.hasNext ()) sqrts.add (Math.sqrt (inp.nextDouble ())); double sum = 0; for (double x : sqrts) sum += x;
- Almost painless, but, alas, expensive.