-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path2. LinkedList
More file actions
55 lines (39 loc) · 1.35 KB
/
2. LinkedList
File metadata and controls
55 lines (39 loc) · 1.35 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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class App {
public static void main(String[] args) {
/*
* ArrayLists manage arrays internally.
* [0][1][2][3][4][5] ....
*/
List<Integer> arrayList = new ArrayList<Integer>();
/*
* LinkedLists consists of elements where each element
* has a reference to the previous and next element
* [0]->[1]->[2] ....
* <- <-
*/
List<Integer> linkedList = new LinkedList<Integer>();
doTimings("ArrayList", arrayList);
doTimings("LinkedList" , linkedList);
}
private static void doTimings(String type, List<Integer> list) {
for(int i=0; i<1E5; i++) {
list.add(i);
}
long start = System.currentTimeMillis();
/*
// Add items at end of list
for(int i=0; i<1E5; i++) {
list.add(i);
}
*/
// Add items elsewhere in list
for(int i=0; i<1E5; i++) {
list.add(0, i);
}
long end = System.currentTimeMillis();
System.out.println("Time taken: " + (end - start) + " ms for " + type);
}
}