| 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 + 1 index |
| 45 | while (iter.hasNext() && i <= mid) { |
| 46 | iter.next(); |
| 47 | i++; |
| 48 | } |
| 49 | iter.add(value); |
| 50 | } |
| 51 | |
| 52 | public void addNEMiddle(int Nelements) { |
| 53 | ListIterator<E> iter = list.listIterator(); |
| 54 | int i = 0; |
| 55 | // go to the element at the middle + 1 index |
| 56 | while (iter.hasNext() && i <= list.size() / 2) { |
| 57 | iter.next(); |
| 58 | i++; |
| 59 | } |
| 60 | for (int j = 0; j < Nelements; j++) { |
| 61 | iter.add((E)new Object()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | } |