Inheritance III: Subtype Polymorphism, Comparators, Comparable 11.1 A Review of Dynamic Method Selection Static Type vs. Dynamic Type static type: 变量声明时指定的类型。 dynamic type: 变量运行时的类型,根据 new 关键字。 11.2 ComparablesSubtype Polymorphism vs Explicit Higher Order Functions Explicit Higher Order Functions: def print_larger(x, y, compare, stringify): if compare(x, y): return stringify(x) return stringify(y) Subtype Polymorphism: def print_larger(x, y): if x.largerThan(y): return x.str() return y.str() 11.3 Comparables java 有一个 comparable interface: public interface Comparable<T> { /* Return negative number if o1 < o2....
Posts
12. Inheritance IV: Iterators, Object Methods 12.1 Lists and Sets in Java list java 提供了 built-in 的 List interface 和一些实现,比如 ArrayList. import java.util.List; import java.util.ArrayList; public class Example { public static void main(String[] args) { List<Integer> L = new ArrayList<>(); L.add(5); L.add(10); System.out.println(L); } } set java 提供了 built-in 的 Set interface 和一些实现,比如 HashSet. import java.util.Set; import java.util.HashSet; Set<String> s = new HashSet<>(); s.add("Tokyo"); s.add("Lagos"); System.out.println(s.contains("Tokyo")); // true 12.2 Exceptions java 抛出异常语法:...
Asymptotics 15.1 For Loops int N = A.length; for (int i = 0; i < N; i += 1) for (int j = i + 1; j < N; j += 1) if (A[i] == A[j]) return true; return false; 有两种方法来渐进分析: Methood 1: 计算运算符的数量 比如看==的执行次数,第一次循环为$N-1$次,第二次为$N-2$次,…,最后一次为$1$次,所以总共为$1+2+3+…+N-1$,即$N(N-1)/2$, 运行时间为 $\theta(N^2)$。 Methood 2: 几何分析 可以看到==的执行次数和三角形的两边 $N-1$ 都有关系,所以面积 in $N^2$ family, 可以得到运行时间为 $\theta(N^2)$ 再看一个例子: public static void printParty(int N) { for (int i = 1; i <= N; i = i * 2) { for (int j = 0; j < i; j += 1) { System....
ADTs and BSTs 16.1 Abstract Data Types 常见的 ADTs 有: Stacks push(int x) pop() Lists add(int i) get(int i) Sets add(int i) contains(int i) Maps put(K key, V value) V get(K key) 16.2 Binary Search Trees 二叉搜索树 node 的左边子节点都比 node 小,右边子节点都比 node 大。 private class BST<Key> { private Key key; private BST left; private BST right; public BST(Key key, BST left, BST Right) { this.key = key; this.left = left; this....
Inheritance I: Interface and Implementation Inheritance 9.2 Hypernyms, Hyponyms, and the Implements Keyword interface 类: public interface List61B<Item> { public void addFirst(Item x); public void add Last(Item y); public Item getFirst(); public Item getLast(); public Item removeLast(); public Item get(int i); public void insert(Item x, int position); public int size(); } 实现类: public class AList<Item> implements List61B<Item>{...} 9.3 Overriding, Interface Inheritance overriding 实现类实现 interface 类的时候要加上@Override关键字。 @Override public void addFirst(Item x) { insert(x, 0); } 9....
Conditional Breakpoints, Execution Breakpoints 的设置. Conditional Breakpoints 对Intellij中的断点右键,会出现 在condition中输入条件表达式,当满足条件时,断点生效。 Execution Breakpoints Run->View Breakpoints, 选择Java Exception Breakpoints, 选择需要断点的异常类型.
A Debugging Mystery 这个问题是因为 Java 中 Integer 对象的缓存机制导致的。 在 Java 中,== 运算符比较的是对象的引用(内存地址),而不是对象的值。当你使用 == 比较两个 Integer 对象时,你实际上是在比较它们是否是同一个对象实例。 Java 为了优化性能,对于范围在 -128 到 127 之间的整数,会预先缓存对应的 Integer 对象。这意味着: 当你创建值在 -128 到 127 范围内的 Integer 对象时,Java 会返回缓存中的对象 当值超出这个范围时(如 128),每次创建都会生成新的对象实例 因此,当你比较两个超出这个范围的 Integer 对象时,它们是不同的对象实例,== 运算符返回 false。 Integer a = 127; Integer b = 127; a == b; // 返回 true,因为 a 和 b 引用同一个缓存对象 Integer c = 128; Integer d = 128; c == d; // 返回 false,因为 c 和 d 是不同的对象实例 所以要比较两个integer大小,需要使用equals方法。...
book 看浮点数实现。 浮点数汇编。 lab data lab 继续做以及笔记。 bomb lab phase6和secret bomb。