Comp 271 Week 6
library sorting
- Jsort: applying Collections.sort to an ArrayList<Integer>
- MyClass: how would we sort these? Having it implements Comparable. This means we must implement compareTo(); note the format here.
- Giving compareTo() output, so we can see what got compared to what
- is mergeSort a good choice here? Note that the data gets copied from the ArrayList to an array, and a second array is allocated.
linked lists
First look at linked lists
Suppose we define
class Cell {
private String data;
private Cell next;
Cell(String s, Cell n) {data = s; next = n;}
String data() {return this.data;}
Cell next(() {return this.next;}
}
What can we do with this? Here is an empty list:
L = null
Here is a one-element list:
L = new Cell("cherry", L)
How about this? In what order do they get added?
L = new Cell("blueberry", L);
L = new Cell("apricot", L);
What does this loop do?
Cell p = L;
while (p!= null) {
System.out.println(p.data());
p = p.next();
}
First examples: LinkedList.demos. These involve "raw" cell manipulations
Second: the LinkedList class, and LLtests
printList
addFirst
the equals() method
third: unit testing
Thursday
Mergesort:
If your mergePass had
while (i<len) { mergeBlocks(...); i +=2*blocksize;}
then you must make the correction I mentioned during the lab on Tuesday.
BUT if your mergePass had
while (i<len-blocksize) {mergeBlocks(...); i+= 2*blocksize;}
then you cease mergeBlocking as soon as you fail to have at least part
of a second block, so the problem case does not arise. This is why my
code (the second form here) worked, when my mergeBlocks() did not work
for many of you.
null objects
It has always been illegal in Java to call any methods on an object
variable until it has been allocated with new. Things are no different
here, except that in linked structures we routinely encounter object variables that are null (not allocated) and always will be. A null variable is to be thought of as a termination marking, not as a sign that something hasn't been allocated yet.
As a consequence, before you call p.data() or p.next(), you should have
verified explicitly in the code that p!=null. Example:
while (p!=null) {foo(p.data()); p = p.next(); }
while (p!=null && p.data != X) p =
p.next; // simple search for X
implementation of get(n)
Unit Test creation
Examples of creating tests using BlueJ and manually
Discussion of completeness of testing: what should we test for?
Note that asking for get(N) for an N-element list is a particularly tricky case!
add a test for reversing an empty list? A one-element list?
add a test for clone
Subtle issue: we also need to modify one list, and verify that the clone is now different! Why is this?
add a remove(T value method, and a test for it.
Note there is a problem with remove: once we find the cell that has the value, we need to modify the previous cell.
And we have to treat the first cell specially: to remove it, we have to modify head.
And we have to treat the empty list as a special case.
in-class project: add a test for insertAfter at the end of a list. That
is, if L is a 3-element list, its last element is #2. Try
L.insertAfter(2,"newvalue").
Look at the test for the iterator.
Create a three-element list, and verify that the iterator values match
the values we know to be there (or that we obtain with get(n).
Add a test for size().
adding a clear() method: what happens to the old cells?
adding a remove(T value)
Negative tests:
I should add tests of cases that should
fail. It is best if these cases throw an exception, so I can catch the
exception to validate the failure. We'll try this later.
In general, test routines can't examine output messages.
java.util.LinkedList
Linked List structures in the Java library: ArrayList v LinkedList, etc
See http://java.sun.com/docs/books/tutorial/collections/implementations/list.html.