0%

cs61b系列day3——ArrayDeque

重点是Java中数组的特点,以及对数据结构细节实现的思考。

概要

  • Reading2.3
    • 着眼于删除链表结点的问题,追求操作的简便与快速,讲述了更多有关链表操作的细节。
    • 简要介绍Java中泛型的用法
  • Reading2.4
    • Java中数组的基础用法(其中System.arraycopy方法比较常用)
    • 二维数组基础
    • Class与Array对比
  • Reading2.5
    • 数组队列中resize方法的分析
    • why null all objects

概念

  1. Class and array

    • array能在运行时制定访问哪个成员,如int i = …, a[i]
    • Class不能,如String s = “item”, p.s会错误
  2. Reflection

    • 利用反射可以在运行时确定访问的成员,但:
    • Don’t use it because of bad coding style

一些感受

ArrayDeque:

  1. 想到一种好的结构能帮你省很多时间。(例如我把first、last作为哨兵结点(也可以不是)、resize时仅仅是遍历后赋值)
  2. 理清逻辑顺序很重要。
  3. 数据结构还是要画图分析比较好。
  4. Project后的tips很有启发性。

Tips

各种各样重要的细节

Appendix: Java Arrays vs. Other Languages

Compared to arrays in other languages, Java arrays:

  • Have no special syntax for “slicing” (such as in Python).
  • Cannot be shrunk or expanded (such as in Ruby).
  • Do not have member methods (such as in Javascript).
  • Must contain values only of the same type (unlike Python).

AList

  • Advantage: faster get than linked-list.
  • Rule: Any change to our list must be reflected in a change in one or more memory boxes in our implementation.
  • Resize: newSize = oldSize * Factor.
    We’ll defer a full analysis(均摊分析) of why this happens until the final chapter of this book.
  • Memory performance:
    In a typical implementation, we halve the size of the array when R falls to less than 0.25.

Generic problems:

There is one significant syntactical difference: Java does not allow us to create an array of generic objects due to an obscure issue with the way generics are implemented.

Make null and remove:

Whereas before, we had no reason to zero out elements that were deleted, with generic objects, we do want to null out references to the objects that we’re storing. This is to avoid “loitering”.(所以对于数组来说,某个储存空间没有引用就是值为null?)


新工具

  1. Java visualizer

    IntelliJ的一个插件,用于Debug时视觉化数据结构、栈等,拥有与网页版基本相同的效果。
  2. Style Checker

    cs61b课程的一个插件,用于检查文档的格式(或者说风格),并定位错误。