forked from hellokaton/write-readable-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample5.java
More file actions
66 lines (53 loc) · 1.41 KB
/
Copy pathExample5.java
File metadata and controls
66 lines (53 loc) · 1.41 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package chapter_04;
/**
* list.size
*
* @author biezhi
* @date 2018/6/27
*/
public class Example5 {
static class List<E> {
private E[] data;
private int pos;
public List(E[] data) {
this.data = data;
}
int countSize() {
int size = 0;
for (E e: data) {
if (null != e) {
size += 1;
}
}
return size;
}
int size() {
return data.length - pos;
}
E popBack() {
pos++;
E lastItem = data[data.length - pos];
data[data.length - pos] = null;
return lastItem;
}
}
private void shrinkList(List<String> list, int maxSize) {
while (list.size() > maxSize) {
freeNode(list.popBack());
}
}
private void freeNode(String item) {
System.out.println("释放了: " + item);
}
public static void main(String[] args) {
String[] items = new String[10_0000];
for (int i = 0; i < items.length; i++) {
items[i] = "hello_#" + i;
}
List<String> list = new List<>(items);
Example5 example5 = new Example5();
long startMs = System.currentTimeMillis();
example5.shrinkList(list, 10);
System.out.println((System.currentTimeMillis() - startMs) + "ms");
}
}