- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayTest.java
More file actions
Latest commit
60 lines (53 loc) · 1.77 KB
/
Copy pathArrayTest.java
File metadata and controls
60 lines (53 loc) · 1.77 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
packagecom.zengrui.java.struct;
/**
* @Description: 数组测试
* @Author: zengrui
* @CreateTime: 2019/10/28 11:39
*/
publicclassArrayTest {
privateint[] array;
privateintcapacity;
publicintsize = 0;
publicArrayTest(intcapacity) {
this.array = newint[capacity];
this.capacity = capacity;
}
publicvoidinsertIntArray(intvalue) {
for (inti = 0; i < capacity; ++i) {
if (this.array[i] == value) {
System.out.println("hit cache: " + value);
return;
}
}
if (size < capacity) {
this.array[size] = value;
size ++;
System.out.println("add to cache: " + value);
} else {
// LRU
System.out.println("expired from cache: " + this.array[0]);
System.arraycopy(this.array, 1, this.array, 0, capacity - 1);
System.out.println("add to cache: " + value);
this.array[capacity - 1] = value;
}
}
publicstaticvoidmain(String[] args) throwsException {
ArrayTestarrTest = newArrayTest(16);
for (inti = 1; i < 17; ++i) {
arrTest.insertIntArray(i);
}
// 缓存命中
arrTest.insertIntArray(10);
arrTest.insertIntArray(3);
// LRU缓存淘汰
arrTest.insertIntArray(17);
// System.copy
ArrayTest[] objectArray = newArrayTest[2];
ArrayTest[] destArray = newArrayTest[2];
objectArray[0] = newArrayTest(100);
objectArray[1] = newArrayTest(200);
System.arraycopy(objectArray, 0, destArray, 0, 2);
objectArray[0].capacity = 101;
System.out.println("Finished.");
}
}