Commit | Line | Data |
---|---|---|
01bad5b3 JB |
1 | import java.util.List; |
2 | import java.util.ListIterator; | |
3 | ||
4 | public class ListExtension<E> { | |
5 | private List<E> list; | |
6 | ||
7 | public ListExtension() { | |
8 | setList(null); | |
9 | } | |
10 | ||
11 | public ListExtension(List<E> l) { | |
12 | setList(l); | |
13 | } | |
14 | ||
15 | /** | |
16 | * [setList description] | |
17 | * @param l [description] | |
18 | */ | |
19 | public void setList(List<E> l) { | |
20 | list = l; | |
21 | } | |
22 | ||
23 | /** | |
24 | * [getList description] | |
25 | * @return [description] | |
26 | */ | |
27 | public List<E> getList() { | |
28 | return list; | |
29 | } | |
30 | ||
31 | public boolean add(E e) { | |
32 | return list.add(e); | |
33 | } | |
34 | ||
35 | /** | |
36 | * Should mimic the List<E> add(int index, T value) method | |
37 | * @param value [description] | |
38 | * @return [description] | |
39 | */ | |
40 | public void addMiddleIter(E value) { | |
41 | int mid = list.size() / 2; | |
42 | ListIterator<E> iter = list.listIterator(); | |
43 | int i = 0; | |
44 | // go to the element at mid index | |
45 | while (iter.hasNext() && i < mid) { | |
46 | iter.next(); | |
47 | i++; | |
48 | } | |
49 | iter.next(); // Insert before mid + 1 | |
50 | iter.add(value); | |
51 | } | |
52 | ||
53 | public void addNEMiddle(int Nelements) { | |
54 | ListIterator<E> iter = list.listIterator(); | |
55 | int i = 0; | |
56 | // go to the element at the middle index | |
57 | while (iter.hasNext() && i < list.size() / 2) { | |
58 | iter.next(); | |
59 | i++; | |
60 | } | |
61 | iter.next(); // Insert before mid + 1 | |
62 | for (int j = 0; j < Nelements; j++) { | |
63 | iter.add((E)new Object()); | |
64 | } | |
65 | } | |
66 | ||
67 | } |