publicclassDogimplementsComparable<Dog> { ... publicintcompareTo(Dog uddaDog) { returnthis.size - uddaDog.size; } privatestaticclassNameComparatorimplementsComparator<Dog> { publicintcompare(Dog a, Dog b) { return a.name.compareTo(b.name); } } publicstatic Comparator<Dog> getNameComparator() { returnnewNameComparator(); } }
Eceptions and Literators
date: 2025-9-26 11:07:16
本节实现数据结构 ArraySet
Exceptions
1
thrownewExceptionObject(parameter1, ...)
1 2 3 4 5 6 7 8 9 10 11 12
/* Associates pthe specified value with the specified key in this map. Throws an IllegalArgumentException if the key is null. */ publicvoidadd(T x) { if (x == null) { thrownewIllegalArgumentException("can't add null"); } if (contains(x)) { return; } items[size] = x; size += 1; }
publicclassArraySet<T> implementsIterable<T> { private T[] items; privateint size; // the next item to be added will be at position `size` publicArraySet() { items = (T[]) newObject[100]; size = 0; } /* Returns true if this map contains a mapping for specified key. */ publicbooleancontains(T x) { for (inti=0; i < size; ++i) { if (items[i].equals(x)) { returntrue; } } returnfalse; } /* Associates the specified value with the specified key in this map. Throws an IllegalArgumentException if the key is null. */ publicvoidadd(T x) { if (x == null) { thrownewIllegalArgumentException("can't add null"); } if (contains(x)) { return; } items[size] = x; size += 1; } /* Returns the number of key-value mappings in this map */ publicintsize() { return size; } /* Returns an iterator into ME */ public Iterator<T> iterator() { returnnewArraySetIterator(); } privateclassArraySetIteratorimplementsIterator<T> { privateint wizPos; publicArraySetIterator() { wizPos = 0; } publicbooleanhasNext() { return wizPos < size; } public T next() { TreturnItem= items[wizPos]; wizPos += 1; return returnItem; } } publicstaticvoidmain(String[] args) { ArraySet<Integer> aset = newArraySet<>(); aset.add(5); aset.add(23); aset.add(42);
// iteration for (int i : aset) { System.out.println(i); } } }
publicclassMaxArrayDequeTest { @Test /** Adds a few things to the list, checking isEmpty() and size() are correct, * finally printing the results. * */ publicvoidaddIsEmptySizeTest() { Comparator<String> c = newComparator<String>() { @Override publicintcompare(String o1, String o2) { return o1.compareTo(o2); } }; MaxArrayDeque<String> mad1 = newMaxArrayDeque<>(c);
assertTrue("A newly initialized MADeque should be empty", mad1.isEmpty()); mad1.addFirst("front");
assertEquals(1, mad1.size()); assertFalse("mad1 should now contain 1 item", mad1.isEmpty());
intsize= mad1.size(); StringerrorMsg=" Bad size returned when removing from empty deque.\n"; errorMsg += " student size() returned " + size + "\n"; errorMsg += " actual size() returned 0\n";
@Override public String toString() { StringBuilderreturnSB=newStringBuilder("{"); for (inti=0; i < size - 1; ++i) { returnSB.append(items[i].toString()); returnSB.append(", "); } returnSB.append(items[size - 1]); returnSB.append("}"); return returnSB.toString(); }
Shorter toString:
1 2 3 4 5 6 7 8
@Override public String toString() { List<String> listOfItems = newArrayList<>(); for (T x : this) { listOfItems.add(x.toString()); } return"{" + String.join(", ", listOfItems) + "}"; }
这样的实现更简洁, 但还是效率低下, 如果不需要频繁调用 toString, 这样写更好看.
equals
1 2 3 4 5 6 7
@Override publicbooleanequals(Object o) { // the parameter must be Object, not ArraySet or something, because this is a overriding method if (o instanceof Dog uddaDog) { returnthis.size == uddaDog.size; } returnfalse; }
instanceof:
checks to see if o’s dynamic type is Dog
implicitly casts o as a Dog into a variable called uddaDog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
@Override publicbooleanequals(Object o) { if (this == o) { returntrue; } if (o instanceof ArraySet oas) { if (this.size != oas.size) { returnfalse; } for (T x : this) { if (!oas.contains(x)) { returnfalse; } } returntrue; } returnfalse; }
.of
var arg (variable number of arguments)
new pattern of generic (of is a static method which can not access generic param T)
1 2 3 4 5 6 7
publicstatic <Glerp> ArraySet<Glerp> of(Glerp... stuff) { // Glerp has nothing to do with T, it's just the declaration that this is a method which is generic ArraySet<Glerp> returnSet = newArraySet<Glerp>(); for (Glerp x : stuff) { returnSet.add(x); } return returnSet; }