CS61B
CS61B
Lecture 3
Lists
In programming languages, a list is an ordered sequence of objects, often represented by comma-separated values between square brackets.
For example: [3, 6, 9, 12, 15]
Lists support a variety of operations, depending on the whims of whoever implemented the list.
- Append an item.
- Retrieve an item by index.
- Remove an item by index or value.
Map(映射)
本质上是一个键值对(key-value pairs)的集合。
1 | import java.util.*; |
Lecture 4
Unit Test
The Selection Sort Algorithm
Selection sorting a list of N items
- Find the smallest item in the list
- Move it to the front
- Selection sort the remaining N-1 items (without touching front item!)
Find the smallest item in the list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24import java.util.*;
public class test {
public static String findSmallest(String[] input) {
int compared = input[0].compareTo(input[1]);
if (compared > 0) {
return input[1];
} else if (compared < 0) {
return input[0];
} else {
return "SAME!";
}
}
void main() {
var strings = new ArrayList<String>(List.of("hello", "apple"));
String result = findSmallest(strings.toArray(new String[0]));
IO.println(result);
}
}Swap
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Absurd's Blog!

