Introduction
Defining and Using Classes¶
Java中的所有代码都必须是一个class的一部分(所以是完全OOP的)
Command Line Arguments:
比如这段代码
会产生这样的效果:
Reference, Recursion¶
这部分的textbook很简单.
List¶
Basics¶
public class IntList {
public int first;
public IntList rest;
public IntList(int f, IntList r) {
first = f;
rest = r;
}
}
List可以这样赋值:
IntList L = new IntList(5, null);
L.rest = new IntList(10,null);
L.rest.rest = new IntList(15,null);
也可以这样初始化;
size和iterativeSize:
public int iterativeSize() {
IntList p = this;
int totalSize = 0;
while (p != null) {
totalSize += 1;
p = p.rest;
}
return totalSize;
}
get方法:
public int recursiveGet(int pos) {
if (pos == 0) {
return this.first;
} else {
return this.rest.iterativeGet(pos - 1);
}
}
public int iterativeGet(int pos) {
IntList p = this;
int t = pos;
while (t > 0) {
p = p.rest;
t--;
}
return p.first;
}
SLLists¶
public class SLList {
public IntNode first;
public SLList(int x) {
first = new IntNode(x, null);
}
}