CS61B 学习笔记
Java 永远复制变量中保存的内容;基本类型变量保存数据本身,对象变量保存对象的引用。
例如调用函数max(a,b),a 和 b 在此之前定义时已经有内存存储了,故在调用时只是把参数的比特复制进函数作用域中,即按值传递。
数组实例化
int[] x;Dog[] Dogs;两条声明都会创建一个64位的内存盒。
2.1 IntList
Section titled “2.1 IntList”public class IntList{ public int first; public IntList rest;
public IntList(int f, IntList r){ first = f; rest = r; }}此内容在 cs61a 中有所记载 —> 神秘连接
上面的代码过于丑陋,因此我们考虑添加 size 方法。使调用 L.size() 时能得到 L 中元素数量。
size 实现方法
public int size(){ if (rest == null){ return 1; } return 1 + this.rest.size();}iterativesize 实现方法
public int iterativesize(){ IntList p = this; int totalsize = 0; while (p != null){ totalSize += 1; p = p.rest; } return totalSize;}简单分析一下
size- 时间复杂度:
- 空间复杂度(容易 stackoverflow):
iterativesize- 时间复杂度:
- 空间复杂度:
2.2 SLList
Section titled “2.2 SLList”在 2.1 中,我们构建了 IntList 类。从技术上说,它可以完成列表应做的所有事情;但在实际使用中,IntList 相当“别扭”,会导致代码难以月的和维护
根本问题在于,
IntList是一种我称为裸递归的数据结构。为了正确使用IntList,程序员即便只完成简单的列表操作,也必须理解并运用递归。这限制了它对初学者的适用性;而且根据IntList类提供了哪些辅助方法,还可能引入一整类棘手的新错误。(懒得写直接复制了)
吸取 IntList 的经验后,我们讲构建一个新类 SLList。
改进 1:重命名
Section titled “改进 1:重命名”原 IntList 类:
public class IntList { public int first; public IntList rest;
public IntList(int f, IntList r) { first = f; rest = r; }...public class IntNode { public int item; public IntNode next;
public IntNode(int i, IntNode n) { item = i; next = n; }}改进 2:添加一层类
Section titled “改进 2:添加一层类”public class SLList { public IntNode first;
public SLList(int x) { first = new IntNode(x, null); }}添加两个简单的方法:addFIRST 和 getFirst
public class SLList { public IntNode first;
public SLList(int x) { first = new IntNode(x, null); }
/** Adds an item to the front of the list */ public void addFirst(int x) { first = new IntNode(x, first); }}/** Retrieves the front item from the list */public int getFirst() { return first.item;}优化后的等价比较
//SLListSLList L = new SLList(15);L.addFirst(10);L.addFirst(5);int x = L.getFirst();
//IntListIntList L = new IntList(15, null);L = new IntList(10, L);L = new IntList(5, L);int x = L.first;两种数据结构的对比图

我们发现,IntList 可能让变量指向 IntList 的中间位置,这并不是一件好事,因为我们仅仅想要加个头节点。
正如奥维德所说,凡人直视神明便会死去。
因此我们采用 SLList 作为中介,这种方式安全且更为简便。
练习1 给 IntList 写一个 addFirst 方法
Section titled “练习1 给 IntList 写一个 addFirst 方法”错误写法
public void addFirst(int x) { this = new IntList(x, this);}
这是由于 `this` 是 `final` 引用,代表当前对象的地址,不能改变他的值(这和c++是一样的)。
丑陋写法```javapublic void addFirst(int x) { this.rest = new IntList(this.first, this.rest); this.first = x;}这将导致每次都要额外做一次打包赋值,并且无法处理空链表。
改进 3:public 与 private
Section titled “改进 3:public 与 private”遗憾的是,用户仍能绕过 SLList, 直接接触数据本身,因此我们可以修改 SLList, 使用 private 关键字声明 first:
public class SLList { private IntNode first;...}改进 4:嵌套类
Section titled “改进 4:嵌套类”目前有两个 .java 文件:IntNode 和 SLList。不过 IntNode 只是配角。
Java 允许在一个类内部嵌入另一个类的额声明。
public class SLList { public class IntNode { public int item; public IntNode next; public IntNode(int i, IntNode n) { item = i; next = n; } }
private IntNode first;
public SLList(int x) { first = new IntNode(x, null); }...如果嵌套类完全不需要使用 SLList 的任何实例方法或实例变量,可以把它声明为 static。意味着静态嵌套类的方法不能访问外部类的成员。
public class SLList { public static class IntNode { public int item; public IntNode next; public IntNode(int i, IntNode n) { item = i; next = n; } }
private IntNode first;...这样可以节省一些内存,因为 IntNode 不需要记录如何访问包围他的 SLList.
上文是 21spring 的课程一部分内容,之后的内容都是 26spring 的
Section titled “上文是 21spring 的课程一部分内容,之后的内容都是 26spring 的”Lecture 7 Resizing and Circular Arrays
Section titled “Lecture 7 Resizing and Circular Arrays”SLList
Section titled “SLList”承接上文SLList,其实SLList就是在裸列表IntList基础上增加了哨兵节点(sentinel)
- 头插法
public void addFirst(int x) { sentinel.next = new IntNode(x, sentinel.next)}本来的sentinel指向第一个节点(Node1),现在创建一个新的节点,指向Node1,然后sentinel指向这个新节点,使新节点成为首节点,实现头插。
- 尾插法 从头遍历到尾,得到尾节点,将尾节点指向新节点,这样需要遍历整个List,显然很麻烦。这时我们考虑添加 Last 节点,每次插入后更新尾节点,大幅减少时间消耗。
DLList
Section titled “DLList”当我们需要删除最后一个元素时,我们的Last节点并不能起到什么作用,我们考虑类似next的指针结构:prev,即指向前一个元素,由此我们实现了双向链表,缩写为 DLList。
与其在双向链表里既实现sentinel又实现Last两个哨兵节点,不如就实现一个哨兵sentinel,然后哨兵的next指向第一个节点,prev指向最后一个节点。
使用泛型整数列表时,不能写基本类型Int,要写类型参数Integer。
用数组 items 保存元素,size 记录放入了几个元素。
public class AList { private int[] items; private int size; public AList() { items = new int [999]; size = 0; } public void addLast(int x) { item[size] = x; size += 1; } public int get(int i) { return items[i]; }}Lecture 8 主要就是讲一下 AList ,数组列表,简单略过
Section titled “Lecture 8 主要就是讲一下 AList ,数组列表,简单略过”Lecture 9 Inheritance 1
Section titled “Lecture 9 Inheritance 1”9.2 Hypernyms, Hyponyms
Section titled “9.2 Hypernyms, Hyponyms”Hypernyms的意思是上位词,比如 Dog 对于Jiwawa、Alaska等狗的品种是上位词。反过来 Hyponyms 是下位词,即狗的不同种之于 Dog