85d48b512ee127807662fae64d40d5a6bdbbd53f
1 import java
.util
.ArrayList
;
2 import java
.util
.LinkedList
;
3 import java
.util
.ListIterator
;
4 import java
.util
.concurrent
.TimeUnit
;
8 private static <E
> void addMiddle(ArrayList
<E
> array
, E value
) {
9 int mid
= array
.size() / 2;
10 array
.add(mid
+ 1, value
);
13 private static <E
> void addMiddle(LinkedList
<E
> list
, E value
) {
14 int mid
= list
.size() / 2;
15 list
.add(mid
+ 1, value
);
19 * Should mimic the collection add(int index, T value) method
20 * @param array [description]
21 * @param value [description]
22 * @return [description]
24 private static <E
> void addMiddleIter(ArrayList
<E
> array
, E value
) {
25 int mid
= array
.size() / 2;
26 ListIterator
<E
> iter
= array
.listIterator();
28 // go to the element at mid index
29 while (iter
.hasNext() && i
< mid
) {
33 iter
.next(); // Insert before mid + 1
39 * Should mimic the collection add(int index, T value) method
40 * @param list [description]
41 * @param value [description]
42 * @return [description]
44 private static <E
> void addMiddleIter(LinkedList
<E
> list
, E value
) {
45 int mid
= list
.size() / 2;
46 ListIterator
<E
> iter
= list
.listIterator();
48 // go to the element at mid index
49 while (iter
.hasNext() && i
< mid
) {
53 iter
.next(); // Insert before mid + 1
57 private static <E
> void addNEMiddle(ArrayList
<E
> array
, int Nelements
) {
58 ListIterator
<E
> iter
= array
.listIterator();
60 // go to the element at the middle index
61 while (iter
.hasNext() && i
< array
.size() / 2) {
65 iter
.next(); // Insert before mid + 1
66 for (int j
= 0; j
< Nelements
; j
++) {
67 iter
.add((E
)new Object());
71 private static <E
> void addNEMiddle(LinkedList
<E
> list
, int Nelements
) {
72 ListIterator
<E
> iter
= list
.listIterator();
74 // go to the element at the middle index
75 while (iter
.hasNext() && i
< list
.size() / 2) {
79 iter
.next(); // Insert before mid + 1
80 for (int j
= 0; j
< Nelements
; j
++) {
81 iter
.add((E
)new Object());
87 * @param String[] args main() function arguments array
89 public static void main(String
[] args
) {
90 ArrayList
<Integer
> array
= new ArrayList
<Integer
>();
91 LinkedList
<Integer
> list
= new LinkedList
<Integer
>();
93 for (int i
= 0; i
< 1000000; i
++) {
98 long startTime
= System
.nanoTime();
99 //for (int i = 0; i < 100000; i++) {
100 // addMiddleIter(array, i);
102 addNEMiddle(array
, 100000);
103 long stopTime
= System
.nanoTime();
104 long execTime
= stopTime
- startTime
;
105 //System.out.println("Insert time in the middle of the ArrayList: " + TimeUnit.NANOSECONDS.toSeconds(execTime) + " s");
106 System
.out
.println("Insert time in the middle of the ArrayList: " + execTime
+ " ns");
108 startTime
= System
.nanoTime();
109 //for (int i = 0; i < 100000; i++) {
110 // addMiddleIter(list, i);
112 addNEMiddle(list
, 100000);
113 stopTime
= System
.nanoTime();
114 execTime
= stopTime
- startTime
;
115 //System.out.println("Insert time in the middle of the LinkedList: " + TimeUnit.NANOSECONDS.toSeconds(execTime) + " s");
116 System
.out
.println("Insert time in the middle of the LinkedList: " + execTime
+ " ns");