blob: ac8f4870616191a414e7d9172e81735b1fc93b38 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
class Sequence<Base> {
private Base[] bases;
private int count;
public int find (Base base){
if (base == null) {
for (int i=0; i<count; i+=1) {
if (bases[i] == null) {
return index;
}
}
} else {
for (int i=0; i<count; i+=1) {
if (Base.equals(bases[i])) {
return index;
}
}
} return -1; //Fail case
}
//While loop is basically an infinitely nexted series of if statements
//(It's called loop unrolling)
//Sometimes the first execution is special (see below)
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append('[');
if (count > 0) {
builder.append(bases[0].toString())
//Add a helper fo the null pointer
for (int i = 1; i < count; i += 1) {
builder.append(" ,");
//Add a helper to not dereference the null pointer
builder.append(bases[i].toString());
}
} builder.append(']')
return builder.toString();
}
//Arrays as sequences
//Add O(n) needed a copy loop
//Delete O(n) needed a copy loop - first element
//Find O(n) used linear search
//Can we go faster? (Yes, probably)
|