CS 367 – Lecture 3
Announcements
Did
you get the test message to cs367-3list?
(I will send notices)
Assignments
in progress: h1 (due 1/27), p1 (due 2/1)
How
many students need a partner for p1? Are
using MacOS?
Unix
commands & programming proverbs
Java reviews: handin topics, which days (
Change
in jdb tutorial handout: modify
.cshrc.local, not .cshrc
Go
over p1, ask about questions; clarify i/o, 3letter
words are real words
cs 367-1, cs367-2, cs367-3: differences, similarities
feedback
from Thursday’s one-minute papers
Read/study next: program
complexity analysis
Review:
Use ADTs
to write good code
Implement each ADT using a
Java class
Example ADT: List
2 implementations of a List
ADT: use an array, use a linked list
public
class List {
private
Object[] items;
private int numItems;
public void
add(Object ob) { …
// basic code for add #1 (review)
public
void add(Object ob) {
items[numItems] = ob; //
fields of item can be modified externally
numItems++;
//
unusual cases: array is full – insert
before basic code
//
must get a new larger array, copy items to the new array, then add ob
if (numItems == items.length)
expandArray();
· do expandArray by hand, then
write the code
1. get a new larger array (twice the size)
2.
copy all items
from “items” to the new array
// System.arrayCopy(array1,
pos1, array2, pos2, numitems)
3. make the new array be the list called “items”
private
void expandArray() {
// doubling is a good size increase
Object [] newArray
= new Object[2*numItems];
System.arrayCopy(items,0,newArray,0,numItems);
items = newArray; //
don’t forget this step!!!
public void add(int pos, Object ob) what do we need here?
· do the operation by hand on the conceptual
picture: add(2, x)
//basic
code, assuming conceptual 1st position is #0
//
step1: move all items in positions pos
through numItems-1
//
step2: put new item in items [pos]
//
step3: increment numItems
· you can write the code!, see the on-line notes
//
code to handle special cases -- insert before step1
//
array is full – call expandArray
//
bad position, pos <0 or pos > numItems:
throw new IndexOutOfBoundsException(); // discuss next
Implementing
the Constructor
public List() {…}
initialize
the fields for an empty list - shown in notes
· set numItems = 0
· set items = null ??
-
not a good idea,
requires another special case for
-
instead: items = new Object[InitSize];
· private static final int InitSize = 100;
private: part of
“internal view”
static: only one per
class
final: value won’t
change
Read Announcements
Iterators – (how many have used an iterator
before?)
What
are they?
objects
that provide a way to access each item in a collection
for an ordered
collection (like a List):
access the item in
position 1 first, then the item in position 2, etc
We
can iterate through a List using the get
operation:
For
(int k=0; k<L.size(); k++) {
Object ob =
L.get(k);
…
}
or,
String S
= (String) (L.get(k))
// need to tell compiler that get will return a String
But
to make the above code more readable, use an Iterator
· Every class (e.g., the List class) that represents a
collection of items should provide a method called “iterator”
that returns an Iterator object:
public Iterator
iterator() {
//implementation below
}
· To iterate through List L:
import java.util.*
Iterator it = L.iterator(); //create the Iterator
while (it.hasNext()) { // see if finished
Object ob = it.next(); // get the next item
…
}
(think of the Iterator as a
“finger” pointing to one item in the list)
L = x y z
execute the while loop by
hand…
Another
way to write the loop that uses the Iterator:
for
(Iterator it = L.iterator();
it.hasNext(); ) {
Object ob = it.next();
…
}
· if it.hasNext() returns
false,
it.next() throws a NoSuchElementException
· Iterator is an interface
defined in java.util
To
implement an Iterator for the List class, must
define:
1. An iterator method in the
List class (that returns an Iterator object)
2. A new class
that implements the Iterator interface, specialized to work with the List class
· supplies the hasNext, next,
and remove methods
· use a meaningful class name; e.g., ListIterator
Implementation
of the iterator method in the List class:
public Iterator iterator()
{
return new ListIterator(this);
}
must
decide how ListIterator will implement hasNext, next, …
· cannot use a pointer to the items in the array, since
that changes when expandArray is called
so, use a pointer to the List itself, and a position of
“current” item
class ListIterator implements Iterator{
private myList;
private myPos;
public ListIterator (List L) {
myList = L;
myPos = 0;
}
//
draw Iterator fields; discuss hasNext(), next()
public Boolean hasNext() {
return (myPos < myList.size());
}
public Object
next() {
// implementation in the class notes
}
}
Exercise
to try in class:
· ArrayList A
· A is a list of Integers
· Given an Integer In
In.intValue() returns an int
Write code that uses A’s iterator to sum values at position 0,2,4,…