AbstractList provides a skeleton implementation for List, which is like this: Public Abstract Class AbstractList Extends AbstractCollection Implements List Inherits AbstractCollection, implements the List interface. Abstract: http://blog.9cbs.net/treeroot/archive/2004/09/11/101622.aspx about list: http://blog.9cbs.net/treeroot/archive/2004/09/14/104638 .aspx
Let's take a look at the methods in this class.
Public Boolean Add (Object O) {Add (Size (), O); Return True;} Direct call method add (int index, object o) insert a data at the end
Abstract public object get (int index); this method is not implemented
Public Object Set (int index, object element) {throw new unsupportedOperationException ();} This method is not supported
Public void add (int index, object element {throw new unsupportedOperationException ();} This method is not supported, and this method directly affects the above PUBLIC Boolean Add (Object O) method.
Public Object Remove (INT INDEX) {throw new unsupportedOperationException ();} This method is not supported
Public int indexof (Object O) {Listiterator E = Listiterator (); if (o == null) {while (E. ShasNext ()) ing (E.NEXT () == null) Return E.PREVIOSDEX ();} Else {while (E.EQUALS (E.NEXT ())) Return E.PREVIOSDEX ();} return -1;} This method gets the index of the specified element in the list, , Found a minimum. If you can't find it, return -1, you can't find the whole list.
Public int LastIndexof (Object O) {Listiterator E = Listiterator (Size ()); if (o == null) {while (E.PREVIOS () == null) Return E.NEXTINDEX } Else {while (E.PREVIOS ()) f (} (E.PREVIOS ())) Return E.NEXTINDEX ();} return -1;} This method is almost the same as above, but It is looking forward to the front, finding a maximum of an index.
Public void clear () {transoveRANGE (0, size ());} All elements between two indexes, including start, not including end, see RemoveRange method.
Public Boolean Addal (INDEX, Collection C) {Boolean Modified = False; Iterator E = C.Iterator (); while (e.hasnext ()) {add (index , e.next ()); modified = true;} Return Modified;} This method is implemented by looping the Add method, since each time the Add method is transferred backward, there is a need to move C.Size (), the efficiency is lower. This method is generally override in the subclass. Public iterator itrator () {return new itr ();} There is an internal class ITR, see the definition below.
Public Listiterator Listiterator () {return listiterator (0);} Returns the default list iterator, the starting position is at the forefront.
Public Listiterator Listiterator (Final Int Index) {IF (INDEX <0 || Index> size ()) throw new indexoutofboundsexception ("INDEX:" index); Return New ListiTR (INDEX);} First check the crossed situation, there is also one Internal class ListiTR, see the definition below.
The following is the definition of ITR: About Iterator interface: http://blog.9cbs.net/treeroot/archive/2004/09/11/101589.aspx private internal class private class itr imports itrator {int Cursor = 0; record cursor position
INT LastRet = -1; the last time called NEXT () or previous () index, actually Iterator does not define previous ().
INT EXPECTEDMODCOUNT = Modcount; Record the number of times, modcount is defined in AbstractList as a structure to change the number of people. Here is to access concurrent access when Iterator and Listiterotor access list, and we will discuss this issue later.
Public Boolean Hasnext () {Return Cursor! = size ();} If the current cursor is not equal to the size of the collection (then a value of 0 to SIZE () - 1) indicates that there is a next value. Size () is a method in AbstractList.
public Object next () {try {Object next = get (cursor); checkForComodification (); lastRet = cursor ; return next;} catch (IndexOutOfBoundsException e) {checkForComodification (); throw new NoSuchElementException ();}} where more annoying It is called the method GET (INDEX) in AbstractList, which captures a system exception (exception that can not capture) indexoutofboundsexception. In any case, I will first throw and send an abnormality concurrentModificationException (if any). Under normal circumstances, the current cursor and the last visit index are plus 1.
Public void remove () {if (Lastret == -1) throw new illegalStateException (); checkforcomodification (); try {AbstractList.this.remove (Lastret The following is the definition of Listitr: About ListIterator interface: http://blog.9cbs.net/treeroot/archive/2004/09/14/104608.aspx Internal private private class listitr extends itr imports listiterator {listitr (int index) {cursor = index;} constructor, initializing the current cursor location. Public Boolean Hasprevious () {return cursor! = 0;} The current cursor is not 0 means an element in front. public Object previous () {try {int i = cursor - 1; Object previous = get (i); checkForComodification (); lastRet = cursor = i; return previous;} catch (IndexOutOfBoundsException e) {checkForComodification (); throw new NoSuchElementException ();}} Returns the previous elements of the current cursor, the wage value, and the last access index have changed, and the concurrent control reference ITR Public int nextindex () {return curSor;} The index of the next element is equal. Public int previousindex () {return cursor-1;} No need to say more public void set (Object o) {if (lastRet == -1) throw new IllegalStateException (); checkForComodification (); try {AbstractList.this.set (lastRet, o); expectedModCount = modCount;} catch (IndexOutOfBoundsException e) { THROW New ConcurrentModificationException ();}} Call the SET method of the peripheral class, concurrently controls the description of the Remove method in the ITR. public void add (Object o) {checkForComodification (); try {AbstractList.this.add (cursor , o); lastRet = -1; expectedModCount = modCount;} catch (IndexOutOfBoundsException e) {throw new ConcurrentModificationException ();}} Reference Description of the Remove method in the ITR. } Back AbstractList method public List subList (int fromIndex, int toIndex) {return (this instanceof RandomAccess new RandomAccessSubList (this, fromIndex, toIndex):? New SubList (this, fromIndex, toIndex));} If the List implements Randomaccess interface, returns a new RandomAccessSublist instance, otherwise returns a sublist instance, which is defined later. Public Boolean Equals (Object O) {IF (o == this) Return True; if (! (o instanceof list) Return False; Listiterator E1 = Listiterator (); Listiterator E2 = ((List) O) .listiterator (); while (e1.hasnext () && e2.hasnext ()) {Object O1 = E1.NEXT (); Object O2 = E2. Next (); if (! (! (1 == null? o2 == null: o1.equals (o2)))) Return False;} Return! (E1.hasNext () || E2.hasnext ());} Compare, by traversing two lists, only two list elements and order are exactly the same. Public Int hashcode () {INT has = iTerator (); while (i.hasnext ()) {Object obj = i.next (); hashcode = 31 * hashcode (Obj == null? 0: Obj.hashcode ());} Return hashcode;} This algorithm can be guaranteed: two List is equal to their HashCode. Protected Void Removerange (int fromindex, int toindex) {listiterator it = listiterator (fromNDEX); for (int i = 0, n = toindex-fromNDEX; I SubList (AbstractList list, int fromIndex, int toIndex) {if (fromIndex <0) throw new IndexOutOfBoundsException ( "fromIndex =" fromIndex); if (toIndex> list.size ()) throw new IndexOutOfBoundsException ( "toIndex =" toIndex ); if (fromIndex> toIndex) throw new IllegalArgumentException ( "fromIndex (" fromIndex ")> toIndex (" toIndex ")"); l = list; offset = fromIndex; size = toIndex - fromIndex; expectedModCount = l .Modcount; Public Object Set (INDEX, Object Element) {RangeCheck (Index); CheckForcomodification (); Return L.SET (INDEX OFFSET, ELEMENT); Public Object Get (int index) {RangeCheck (Index); CheckForcomodification (); return L.Get (INDEX OFFSET); Public int size () {CheckforComodification (); return size;} public void add (int index, Object element) {if (index <0 || index> size) throw new IndexOutOfBoundsException (); checkForComodification (); l.add (index offset, element); expectedModCount = l.modCount; size ; modCount ;} public Object remove (int index) {rangeCheck (index); checkForComodification (); Object result = l.remove (index offset); expectedModCount = l.modCount; size--; modCount ; return result;} protected void removeRange (int fromIndex, int toIndex) {checkForComodification (); l.removeRange (fromIndex offset, toIndex offset); expectedModCount = l.modCount; size - = (toIndex-fromIndex); modCount ;} Public Boolean Addall (Collection C) {Return Add (Size, C);} Public Boolean Addall (INDEX, Collection C) {if (INDEX <0 || index> size) throw new indexoutofboundsexception ("INDEX:" INDEX ", SIZE:" size); int csize = C.Size (); IF (CSIZE == 0) Return False; CheckforComodification (); l.addall (offset index, c); expectedmodcount = l.modcount; size = csize; modcount ; return true;} Public iterator itrator () {return listiterator (); Public Listiterator ListIterator (Final INDEX) {CheckForcomodification (); if (Index <0 || Index> Size) Throw new indexoutofboundsexception ("INDEX:" INDEX ", SIZE:" size); Return new Listiterator () {private listiterator i = l.listiterator (index offset); public boolean hasnext () {return nextindex () Private Void Rangecheck (int index) {if (INDEX <0 || index> = size) throw new indexoutofboundsexception ("INDEX:" INDEX ", size:" size);} private void checkForComodification () {if (! l.modCount = expectedModCount) throw new ConcurrentModificationException ();}} class inherits AbstractList, substantially well understood, but there are several major need: 1 Note l.modCount, modCount,. EXPECTEDMODCOUNT, MODCOUNT is Sublist inherited domain EXPECTEDMODCOUNT is Sublist to prevent concurrent access to new domains, L.Modcount is of course well understood. The 2.Public ListItemrator Listiterator (Final INDEX) method is anonymous. 3. Note that the Sublist's constructor is only one, and you need to bring three parameters, and the Sublist is just a view, and the Sublist is also equal to modifying the list in the parameter. Finally, RandomaccessSublistClass RandomaccessSublist Extends Sublist Implements Randomaccess {RandomaccessSublist (AbstractList List, Int fromNDEX, INT TOINDEX) {Super (List, fromNDEX, TOINDEX); Public List Sublist (int fromindex, int toindex) {return new randomaccessSublist (this, fromNDEX, TOINDEX);} This class is not really something, but it is different from Sublist (because there is a randomaccess interface). here Just analyzing this type of implementation, there is no evaluation of this kind of design, but I am more annoying nested class (especially the nested class can call the peripheral method), and the other Sublist returns a view, and Not a completely independent list, is it good?