Computer Science 15-100 (Sections T & U), Spring 2008
Class Notes: Writing Custom ArrayLists (Day 2)
Logistics
Topic Outline:
Code from class:
import java.util.*;
@SuppressWarnings("unchecked")
public class ComparableAndComparatorExample {
public static void main(String[] args) throws Exception {
MutableInteger[] a = new MutableInteger[5];
for (int i=0; i<a.length; i++) a[i] = new MutableInteger(3-17*i % 7);
System.out.println(Arrays.toString(a));
Arrays.sort(a);
System.out.println(Arrays.toString(a));
Integer[] b = { -3, -2, 0, 1, 3};
/*
AbsValComparator comp = new AbsValComparator();
Arrays.sort(b,comp); // sort with an UNNATURAL ordering (by absval!)
System.out.println(Arrays.toString(b));
AbsValComparatorForMutableInteger comp2 = new AbsValComparatorForMutableInteger();
Arrays.sort(a,comp2); // sort with an UNNATURAL ordering (by absval!)
System.out.println(Arrays.toString(a));
*/
UberAbsValComparator comp = new UberAbsValComparator();
Arrays.sort(b,comp); // sort with an UNNATURAL ordering (by absval!)
System.out.println(Arrays.toString(b));
Arrays.sort(a,comp); // sort with an UNNATURAL ordering (by absval!)
System.out.println(Arrays.toString(a));
int[] c = { 5, 3, 1, -2, -4 };
Arrays.sort(c,comp); // sort with an UNNATURAL ordering (by absval!)
System.out.println(Arrays.toString(c));
}
public static void main1(String[] args) throws Exception {
}
}
class UberAbsValComparator implements Comparator {
public int compare(Object o1, Object o2) {
if (o1 instanceof Integer) {
Integer i1 = (Integer)o1;
Integer i2 = (Integer)o2;
return (Math.abs(i1) - Math.abs(i2));
}
else if (o1 instanceof MutableInteger) {
MutableInteger i1 = (MutableInteger)o1;
MutableInteger i2 = (MutableInteger)o2;
return (Math.abs(i1.getValue()) - Math.abs(i2.getValue()));
}
else
throw new RuntimeException("Unknown type: " + o1.getClass());
}
}
class AbsValComparator implements Comparator {
public int compare(Object o1, Object o2) {
Integer i1 = (Integer)o1;
Integer i2 = (Integer)o2;
return (Math.abs(i1) - Math.abs(i2));
}
}
class AbsValComparatorForMutableInteger implements Comparator {
public int compare(Object o1, Object o2) {
MutableInteger i1 = (MutableInteger)o1;
MutableInteger i2 = (MutableInteger)o2;
return (Math.abs(i1.getValue()) - Math.abs(i2.getValue()));
}
}
class MutableInteger extends Object implements Comparable {
private int value;
public MutableInteger() {
setValue(0);
}
public MutableInteger(int value) {
setValue(value);
}
public int compareTo(Object obj) {
MutableInteger that = (MutableInteger) obj;
// return a negative # if (this < that)
// return zero if (this == that)
// return a positive # if (this > that)
return (this.value - that.value);
}
public String toString() {
return "mi(" + this.value + ")";
}
public int inc() { return ++this.value; }
public int getValue() { return this.value; }
public void setValue(int value) { this.value = value; }
}
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem