Berkeley CS61B Spring 2019 打卡(2)

这篇博客记录了Berkeley CS61B春季课程的学习进度,涵盖了子类型多态性、Java中的列表和集合、渐进行为、离散集合、ADTs、BSTs、B-Tree和红黑树等主题。重点讨论了这些数据结构的实现、性能分析和比较。
摘要由CSDN通过智能技术生成

刷题记录在Dropbox Paper上

2019 June 25 || Subtype Polymorphism vs. HoFs

  • Subtype Polymorphism vs. HoFs
  1. Dynamic Method Selection Puzzle
  2. Subtype Polymorphism vs. Explicit HoFs
  3. Application 1: Comparables
  4. 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 on Comparable interface and its compareTo method.
import java.util.Comparator;
public interface Comparator<T> {
	int compare(T o1, T o2);
}
  • Interfaces provide us with the ability to make callbacks:
  1. 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”.
  2. Some languages handle this using explicit function passing.
  3. 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)
  4. 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,
  1. .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.
  2. You should use the according non-primitive types, like using Integer for int.
  • Any class in java is derived from Object class.

  • Generic type variable / Actual type variable
    在这里插入图片描述

  • Exceptions: Something

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值