仅供自勉和记录所用, copyright is held by Berkeley CS61B
- 刷题记录在Dropbox Paper上
-
- 2019 June 25 || Subtype Polymorphism vs. HoFs
- 2019 June 26 || Lists and Sets in Java
- 2019 June 27 || Asymptotics I
- 2019 June 27 || Disjoint Sets
- 2019 June 28 || Asymptotics II
- 2019 June 28 || ADTs, Sets, Maps, BSTs
- 2019 June 29 || B-Trees
- 2019 June 29 || Red Black Trees
- 2019 June 29 || Hashing
- 2019 June 30 || Heaps and PQs
- 2019 July 1 || Prefix Operations and Tries
刷题记录在Dropbox Paper上
2019 June 25 || Subtype Polymorphism vs. HoFs
- Subtype Polymorphism vs. HoFs
- Dynamic Method Selection Puzzle
- Subtype Polymorphism vs. Explicit HoFs
- Application 1: Comparables
- Application 2: Comparators
- Casting is just a trick for compiler; It DOESN’T do any practical thing.
- Polymorphism: “providing a single interface to entities of different types”
- Using built-in Comparable interface And compareTo has many implementations such as for Strings.
public interface Comparable<T> {
public int compareTo(T obj);
}
- For comparison under different criteria, use
Comparator
interface. It is built onComparable
interface and itscompareTo
method.
import java.util.Comparator;
public interface Comparator<T> {
int compare(T o1, T o2);
}
- Interfaces provide us with the ability to make callbacks:
- Sometimes a function needs the help of another function that might not have been written yet.
< Example: max needs compareTo
< The helping function is sometimes called a “callback”. - Some languages handle this using explicit function passing.
- In Java, we do this by wrapping up the needed function in an interface (e.g. Arrays.sort < needs compare which lives inside the comparator interface)
- Arrays.sort “calls back” whenever it needs a comparison.
< Similar to giving your number to someone if they need information.
< See Project 1B to explore how to write code that uses comparators.
2019 June 26 || Lists and Sets in Java
- Primitive type cannot be dereferenced! (cannot be treated as an ordinary Object)
Thus,
.equals(Object)
cannot be used to the primitive types. AND it is not usable for user-defined classes, so we need to override the default equals() method.- You should use the according non-primitive types, like using
Integer
forint
.
-
Any class in java is derived from
Object
class. -
Generic type variable / Actual type variable
-
Exceptions: Something