Java泛型

擦拭法 编译器把类型<T>视为Object public class Pair<T> { private final T first; private final T last; public Pair(T first, T last) { this.first = first; this.last = last; } public T getFirst() { return first; } public T getLast() { return last; } } Java中泛型是在编译阶段的,编译器将上述代码经过擦除法,JVM实际看到的代码如下所示: public class Pair { private final Object first; private final Object last; public Pair(Object first, Object last) { this.first = first; this.last = last; } public Object getFirst() { return first; } public Object getLast() { return last; } } 编译器根据<T>实现安全的强制转型 Pair<String> p = new Pair<>("Hello", "world"); String first = p....

August 4, 2022 · 4 min · fffzlfk

算法(第四版)

The solution of algs4’s exercise

October 9, 2020 · 36 min · fffzlfk