[关闭]
@xmruibi 2015-02-14T18:18:40.000000Z 字数 8186 阅读 1650

List & Abstract List & Abstract Sequential List

Data_Structure


1. List Interface:

  1. public interface List<E> extends Collection<E> {
  2. /****** Extends Collection Interface ******/
  3. int size();
  4. boolean isEmpty();
  5. boolean contains(Object o);
  6. Iterator<E> iterator();
  7. Object[] toArray();
  8. <T> T[] toArray(T[] a);
  9. boolean add(E e);
  10. boolean remove(Object o);
  11. boolean containsAll(Collection<?> c);
  12. boolean addAll(Collection<? extends E> c);
  13. boolean addAll(int index, Collection<? extends E> c);
  14. boolean removeAll(Collection<?> c);
  15. boolean retainAll(Collection<?> c);
  16. void clear();
  17. boolean equals(Object o);
  18. int hashCode();
  19. /***** Implementations of List Interface *****/
  20. E get(int index);// get the index-th element
  21. E set(int index, E element);//set index-th element as new one
  22. void add(int index, E element);//insert the element before the original index-th element
  23. E remove(int index);// remove the index-th element
  24. int indexOf(Object o);// get index from the head
  25. int lastIndexOf(Object o);// get the index from the last
  26. ListIterator<E> listIterator();//返回系列表迭代器
  27. ListIterator<E> listIterator(int index);//从第index开始的系列表迭代器,该迭代器介绍如下
  28. List<E> subList(int fromIndex, int toIndex);//a kind of replica for current list, it isn't a new instance, but reference to original list elements. So any operation on sublist can effect the original element.
  29. }

2. AbstractList & AbstractSequentialList

AbstractList: is to achieve main implementation of List interface, reduce the workload of "Random Access" data collection (ArrayList).

AbstractSequentialList: for "Consecutive Access" data collection (LinkedList)

3. AbstractList

  1. public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>{
  2. protected AbstractList() {//提供一个空的构造函数
  3. }
  4. /**** No implemented ****/
  5. public boolean add(E e) {//在尾部添加元素e
  6. add(size(), e);//在尾部执行添加操作
  7. return true;
  8. }
  9. abstract public E get(int index);
  10. public E set(int index, E element) {
  11. throw new UnsupportedOperationException();
  12. }
  13. public void add(int index, E element) {
  14. throw new UnsupportedOperationException();
  15. }
  16. public E remove(int index) {
  17. throw new UnsupportedOperationException();
  18. }
  19. /*********** Notice the following implemention are relied on list iterator ***********/
  20. // Get the index of given object from the collection beginning
  21. public int indexOf(Object o) {
  22. ListIterator<E> it = listIterator();
  23. if (o==null) {
  24. while (it.hasNext())
  25. if (it.next()==null)
  26. return it.previousIndex();
  27. } else {
  28. while (it.hasNext())
  29. if (o.equals(it.next()))
  30. return it.previousIndex();
  31. }
  32. return -1;
  33. }
  34. // Get the index of given object from the collection end
  35. public int lastIndexOf(Object o) {
  36. ListIterator<E> it = listIterator(size());
  37. if (o==null) {
  38. while (it.hasPrevious())
  39. if (it.previous()==null)
  40. return it.nextIndex();
  41. } else {
  42. while (it.hasPrevious())
  43. if (o.equals(it.previous()))
  44. return it.nextIndex();
  45. }
  46. return -1;
  47. }
  48. /*******************/
  49. public void clear() {
  50. removeRange(0, size());
  51. }
  52. public boolean addAll(int index, Collection<? extends E> c) {
  53. rangeCheckForAdd(index);
  54. boolean modified = false;
  55. for (E e : c) {
  56. add(index++, e);
  57. modified = true;
  58. }
  59. return modified;
  60. }
  61. /************** Forward-Direction ***************/
  62. public Iterator<E> iterator() {//
  63. return new Itr();
  64. }
  65. private class Itr implements Iterator<E> {
  66. int cursor = 0;// current position
  67. int lastRet = -1;//last position of cursor
  68. int expectedModCount = modCount;//for thread safe
  69. public boolean hasNext() {
  70. return cursor != size();//just check the cursor is not in the last
  71. }
  72. // One direction
  73. public E next() {
  74. checkForComodification();
  75. // for concurrency to check if the list changed during iteration
  76. try {
  77. int i = cursor;// current position
  78. E next = get(i);// get the element by position
  79. lastRet = i;// record last position
  80. cursor = i + 1;// new positioin (next position)
  81. return next;
  82. } catch (IndexOutOfBoundsException e) {
  83. checkForComodification();//why check again???
  84. throw new NoSuchElementException();
  85. }
  86. }
  87. //remove method inside the iterator
  88. public void remove() {
  89. if (lastRet < 0)//only can remove the visited element
  90. throw new IllegalStateException();
  91. checkForComodification();
  92. try {
  93. AbstractList.this.remove(lastRet);//parent interface remove implmentation
  94. if (lastRet < cursor)
  95. cursor--; // back the current position
  96. lastRet = -1;//back the lastRet
  97. expectedModCount = modCount;//modified expectedModCount
  98. } catch (IndexOutOfBoundsException e) {
  99. throw new ConcurrentModificationException();
  100. }
  101. }
  102. /****************** For concurrency check *******************/
  103. final void checkForComodification() {
  104. if (modCount != expectedModCount)
  105. throw new ConcurrentModificationException();
  106. }
  107. }
  108. /************** Bidirectional ***************/
  109. public ListIterator<E> listIterator() {// default from 0 (beginning)
  110. return listIterator(0);
  111. }
  112. public ListIterator<E> listIterator(final int index) {
  113. rangeCheckForAdd(index);//inside the list
  114. return new ListItr(index);
  115. }
  116. //Bidirectional iterator extends Forward-Iterator
  117. private class ListItr extends Itr implements ListIterator<E> {
  118. ListItr(int index) {// initial by given index
  119. cursor = index;// set current cursor as given index
  120. }
  121. public boolean hasPrevious() {
  122. return cursor != 0;
  123. }
  124. public E previous() {
  125. checkForComodification();// for concurrency
  126. try {
  127. int i = cursor - 1;
  128. E previous = get(i);
  129. lastRet = cursor = i;//lastRet always for the last visited position
  130. return previous;
  131. } catch (IndexOutOfBoundsException e) {
  132. checkForComodification();
  133. throw new NoSuchElementException();
  134. }
  135. }
  136. public int nextIndex() {
  137. return cursor;
  138. }
  139. public int previousIndex() {
  140. return cursor-1;
  141. }
  142. /*******************Setter and Add implementation in iterator **********************/
  143. public void set(E e) {
  144. if (lastRet < 0)
  145. throw new IllegalStateException();
  146. checkForComodification();
  147. try {
  148. AbstractList.this.set(lastRet, e); // get parent interface to set element
  149. expectedModCount = modCount; //modify expectedModCount
  150. } catch (IndexOutOfBoundsException ex) {
  151. throw new ConcurrentModificationException();
  152. }
  153. }
  154. public void add(E e) {
  155. checkForComodification();
  156. try {
  157. int i = cursor;
  158. AbstractList.this.add(i, e);
  159. lastRet = -1;
  160. cursor = i + 1;
  161. expectedModCount = modCount;
  162. } catch (IndexOutOfBoundsException ex) {
  163. throw new ConcurrentModificationException();
  164. }
  165. }
  166. }
  167. /************** Return sublist with beginning index and ended index ***************/
  168. public List<E> subList(int fromIndex, int toIndex) {
  169. return (this instanceof RandomAccess ?new RandomAccessSubList<>(this, fromIndex, toIndex) :
  170. new SubList<>(this, fromIndex, toIndex));
  171. }
  172. //Use iterator for comparasion
  173. public boolean equals(Object o) {
  174. if (o == this)
  175. return true;
  176. if (!(o instanceof List))
  177. return false;
  178. ListIterator<E> e1 = listIterator();
  179. ListIterator e2 = ((List) o).listIterator();
  180. while (e1.hasNext() && e2.hasNext()) {
  181. E o1 = e1.next();
  182. Object o2 = e2.next();
  183. if (!(o1==null ? o2==null : o1.equals(o2)))
  184. return false;
  185. }
  186. return !(e1.hasNext() || e2.hasNext());
  187. }
  188. /******************** Learn more about hashcode algorithm ********************/
  189. public int hashCode() {
  190. int hashCode = 1;
  191. for (E e : this)
  192. hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
  193. return hashCode;
  194. }
  195. /******************* Remove elements by range *******************/
  196. protected void removeRange(int fromIndex, int toIndex) {
  197. ListIterator<E> it = listIterator(fromIndex);
  198. for (int i=0, n=toIndex-fromIndex; i<n; i++) {
  199. it.next();
  200. it.remove();
  201. }
  202. }
  203. protected transient int modCount = 0; // notice the transient keyword
  204. private void rangeCheckForAdd(int index) {
  205. if (index < 0 || index > size())
  206. throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
  207. }
  208. private String outOfBoundsMsg(int index) {
  209. return "Index: "+index+", Size: "+size();
  210. }
  211. }

4. AbstractSequantialList

  1. // Doesn't support random access, but similar with linked list which is also the abstract class for linkedlist
  2. public abstract class AbstractSequentialList<E> extends AbstractList<E> {
  3. protected AbstractSequentialList() {//提供一个空的默认构造函数
  4. }
  5. /****************** Through Iterator to achieve getter and setter ******************/
  6. public E get(int index) {
  7. try {
  8. return listIterator(index).next();
  9. } catch (NoSuchElementException exc) {
  10. throw new IndexOutOfBoundsException("Index: "+index);
  11. }
  12. }
  13. public E set(int index, E element) {
  14. try {
  15. ListIterator<E> e = listIterator(index);
  16. E oldVal = e.next();
  17. e.set(element);
  18. return oldVal;
  19. } catch (NoSuchElementException exc) {
  20. throw new IndexOutOfBoundsException("Index: "+index);
  21. }
  22. }
  23. /********************** Add and Remove by Iterator ************************/
  24. public void add(int index, E element) {
  25. try {
  26. listIterator(index).add(element);
  27. } catch (NoSuchElementException exc) {
  28. throw new IndexOutOfBoundsException("Index: "+index);
  29. }
  30. }
  31. public E remove(int index) {
  32. try {
  33. ListIterator<E> e = listIterator(index);
  34. E outCast = e.next();
  35. e.remove();
  36. return outCast;
  37. } catch (NoSuchElementException exc) {
  38. throw new IndexOutOfBoundsException("Index: "+index);
  39. }
  40. }
  41. public boolean addAll(int index, Collection<? extends E> c) {
  42. try {
  43. boolean modified = false; // mark the modified
  44. ListIterator<E> e1 = listIterator(index);
  45. Iterator<? extends E> e2 = c.iterator();
  46. while (e2.hasNext()) {
  47. e1.add(e2.next());
  48. modified = true;
  49. }
  50. return modified;
  51. } catch (NoSuchElementException exc) {
  52. throw new IndexOutOfBoundsException("Index: "+index);
  53. }
  54. }
  55. /***************** Iterator Part ********************/
  56. public Iterator<E> iterator() {
  57. return listIterator();
  58. }
  59. //Bidrectional Iterator
  60. public abstract ListIterator<E> listIterator(int index);
  61. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注