Java Collection Abstractlist abstract class __java

Source: Internet
Author: User
Tags addall int size shallow copy concurrentmodificationexception
This class provides the backbone implementation of the List interface to minimize the work required to implement this interface supported by the random Access data store (for an array). For sequential access data, such as linked lists, priority should be given to the use of abstractsequentiallist instead of such jdk1.7 java.util public abstract class ABSTRACTLIST&L T E> extends abstractcollection<e> implements list<e> {protected abstractlist () {} public Boole
        An Add (e e) {//Add the specified element to the tail add (size (), E) of this list;
    return true;
        Abstract public e get (int index)//Returns the element public e set (int index, e Element) of the specified position in the list, throws an exception directly with the specified element replacing the element at the specified position in the list
    throw new Unsupportedoperationexception ();
    public void Add (int index, E Element) {//inserts the specified element at the specified position in the list, throws the exception throw the new Unsupportedoperationexception () directly;
    Public E-Remove (int index) {//Remove the element at the specified position in the list, throw the exception directly throw new Unsupportedoperationexception (); public int indexOf (Object o) {//returns the index of the specified element that appears for the first time in this list, and does not include this element returns-1 listiterator<e> it = Listiterator ();//
          Gets the internal implementation of the bidirectional iterator if (o==null) {while it.hasnext ())      if (It.next () ==null) return It.previousindex (); else {while (It.hasnext ()) if (O.equals (It.next ())) return It.previousi
        Ndex ();
    } return-1; public int LastIndexOf (Object o) {//returns the index of the last occurrence of the specified element in this list, and does not include this element returns-1 listiterator<e> it = Listiterator (size ());//Get internal implementation bidirectional iterator if (o==null) {while (it.hasprevious ()) if (It.previous ()) ==nul
        L) return It.nextindex (); else {while (it.hasprevious ()) if (O.equals (It.previous ())) return IT.N
        Extindex ();
    } return-1;
    public void Clear () {//Empty removerange (0, size ()); public boolean addall (int index, COLLECTION&LT;? extends e> c) {//To insert all elements in the specified Collection into the list at the specified location range
        Checkforadd (index);//Parameter validity check Boolean modified = false; for (E e:c) {ADD (index++, E);
        Modified = true; Return modified;//success Returns true} public iterator<e> iterator () {//Get the iterator for this list back to new Itr (
    );
    The public listiterator<e> Listiterator () {//Gets the bidirectional iterator return Listiterator (0) of the list; //Returns the list iterator (in the appropriate order) of the elements in the list, starting at the specified position in the list. The index specified indicates that the first element returned by the initial invocation of next is the public listiterator<e> listiterator (final int index) {rangecheckforadd (index
    )//Parameter validity check return new LISTITR (index); protected transient int modcount = 0;//The actual number of changes. Transient means no serialization private class Itr implements Iterator<e> {//Internal iterator implementation, inheritance iterator interface int cursor = 0; The cursor, the next index of the element to be accessed int lastret = -1;//represents the index int expectedmodcount = modcount;//of the last accessed element to indicate the expected value of the ArrayList modification. Its initial value is Modcount (indicates the actual number of changes), and the initial value is 0 public boolean hasnext () {//whether there are elements that can be iterated return cursor!= size ();//When the cursor is not equal to a Returns true} public E next () {//next method//Judge Expectedmodcount isNo equals Modcount, when the iterator is invoked, the Add or Remove method in the collection is used to make an error, and the Remove method in the iterator should be used checkforcomodification ();
                try {int i = cursor;
                E next = Get (i); Gets the current value Lastret = i;//Update Lastret cursor = i + 1;//The index of the next element to be iterated
            return to Next;
                catch (Indexoutofboundsexception e) {checkforcomodification ();
            throw new Nosuchelementexception (); }} public void Remove () {//Remove if (Lastret < 0) throw new ILLEGALSTATEEXCEP
            tion ();
            	Determines whether the Expectedmodcount (expected modification) is equal to Modcount (the actual number of modifications), and throws an exception checkforcomodification ().//try { Call the Remove method of the external class, add 1 to Modcount, and the Lastret initial value is-1, you must call the next method to invoke the Remove method AbstractList.this.remove (last
                Ret); if (Lastret < cursor) cursor--;//let cursor minus 1 to ensure that the next iteration after the deletion element is the correct element lastret = -1;//to Last RET REAssignment Expectedmodcount = modcount;//plus 1, so this requires a re assignment} catch (Indexoutofboundsexception e) {
            throw new Concurrentmodificationexception (); The final void Checkforcomodification () {//checksum Expectedmodcount (expected modification) is equal to Modcount (actual number of changes) if (
        Modcount!= expectedmodcount) throw new Concurrentmodificationexception (); }//implement bidirectional iterator interface private class Listitr extends Itr implements listiterator<e> {listitr (int index  {cursor = index;//constructor, where the iteration is started} public boolean hasprevious () {return cursor!= 0;//index is 0 o'clock, there is no previous element} public E Previous () {//Reverse iteration checkforcomodification ();//Checksum Expectedmodcou NT (expected to modify) is equal to Modcount (actual number of changes), the same principle as the previous try {int i = cursor-1;//index-1 E Previou
     s = Get (i);//FETCH element Lastret = cursor = i;//Re-assign return previous;       catch (Indexoutofboundsexception e) {checkforcomodification ();
            throw new Nosuchelementexception ();
        } public int Nextindex () {//Next index return cursor;
        public int Previousindex () {//previous index return cursor-1; The index of the public void set (E e) {if (Lastret < 0)/Set point is less than 0 throws an exception throw new Illegalstateexce
                Ption ()//Checkforcomodification ()//check Expectedmodcount (expected number of changes) equals Modcount (actual number of changes), same principle as previous try { AbstractList.this.set (Lastret, E), or calling the set of the external class to modify, will let the Modcount plus 1, so you need to reassign the value Expectedmodcount =
            Modcount;
            The catch (Indexoutofboundsexception ex) {throw new concurrentmodificationexception (); } public void Add (e e) {//Increase checkforcomodification () at the position of the iteration;//checksum Expectedmodcount (expected number of changes) is equal to
            Modcount (actual number of changes), the same principle as the previous try {    int i = cursor; AbstractList.this.add (i, E);/Call the add of the external class to modify, will let Modcount plus 1, so need to reassign Lastret = -1;//after each increment lastret assignment is 1, so can not immediately
                To delete, you need to recall the next method cursor = i + 1;
            Expectedmodcount = Modcount;
            The catch (Indexoutofboundsexception ex) {throw new concurrentmodificationexception ();
        }} public Boolean equals (Object o) {if (o = = this)//reference equals must be equal to return true; if (!) (
        o instanceof list)/Whether it is a list type or its subtype return false;
        listiterator<e> e1 = Listiterator ();
        Listiterator e2 = ((List) O). Listiterator ();
            while (E1.hasnext () && e2.hasnext ()) {E O1 = E1.next ();
            Object O2 = E2.next (); if (!) ( O1==null?
        O2==null:o1.equals (O2))//every element equal return false; } return! (E1.hasnext () | | e2.hasnext ())//If the number of elements is unequal} public int hashcode () {//HASHCOde Compute int hashcode = 1;
        for (E e:this) hashcode = 31*hashcode + (e==null? 0:e.hashcode ());
    return hashcode; } protected void RemoveRange (int fromindex, int toindex) {//Remove elements that are directly from Fromindex (including) to Toindex (not included) in the collection Listiterator <E> it = Listiterator (Fromindex), starting at the current position for (int i=0, n=toindex-fromindex; i<n; i++) {i
            T.next ();
        It.remove ();
            } private void Rangecheckforadd (int index) {//Parameter range Check if (Index < 0 | | index > Size ())
    throw new Indexoutofboundsexception (Outofboundsmsg (index));
    Private String outofboundsmsg (int index) {//Encapsulation exception Information return "index:" +index+ ", Size:" +size (); }//Gets the new collection from Fromindex (including) to Toindex (excluding) elements in the collection, is a shallow copy, and references affect each other on the public list<e> sublist (int fromindex, int toindex) {/ The/randomaccess interface is the tag interface used by the list implementation to indicate that it supports fast (usually fixed-time) random access///in the Special traversal algorithm for the list, as far as possible to determine whether it belongs to the randomaccess (such as ArrayList) or sequenceaccess (e.g. LinkedList)//Because of the suitabilityRandomaccess List of the traversal algorithm, used in the Sequenceaccess list on the difference is very big return (this instanceof randomaccess?
            Only ArrayList implements the Randomaccess interface and determines whether it is ArrayList or its subclass new randomaccesssublist<> (this, Fromindex, Toindex):
New Sublist<> (This, Fromindex, Toindex)); Class Sublist<e> extends Abstractlist<e> {Private final abstractlist<e> l;//l simply copied the reference to the original list, so the Access to the L will directly access the external classes, accessing different elements through different indexes private final int offset;//offset private int size;//size sublist (ABSTRACTLIST&LT;E&G T list, int fromindex, int toindex) {if (Fromindex < 0) throw new Indexoutofboundsexception ("Fromind
        ex = "+ Fromindex");
        if (Toindex > List.size ()) throw new Indexoutofboundsexception ("Toindex =" + Toindex);
                                               if (Fromindex > Toindex) throw new IllegalArgumentException ("Fromindex (" + Fromindex +
        ") > Toindex (" + Toindex + ")); L = list;//Copy original list primerWith offset = fromindex;//offset size = toindex-fromindex;//size//this.modcount access to its own modcount;l.modcount
    Access to the outer class Modcount (L is a reference to the external class, directly copying the reference) This.modcount = L.modcount; Public E Set (int index, E Element) {//Modify element Rangecheck (index);//parameter checksum//determine if This.modcount equals L.modcount
        , if the external class set is modified with sublist (which affects the Modcount method), the exception Checkforcomodification () is thrown;
    Return L.set (index+offset, Element); Public E gets (int index) {//Get element Rangecheck (index);//parameter checksum//This.modcount is equal to L.modcount if used with sublist
        When the external class collection is modified (which affects the Modcount method), throws an exception checkforcomodification ();
    Return L.get (Index+offset);
        public int size () {//Size///judge whether This.modcount equals L.modcount, throw an exception if you modify the collection of external classes with sublist (which affects the Modcount method)
        Checkforcomodification ();
    return size;
      public void Add (int index, E Element) {//new element Rangecheckforadd (index); To determine whether This.modcount equals L.modcount, if you use sublist to set the outer class into theRow modification (which affects the Modcount method), throws an exception checkforcomodification ();
        L.add (index+offset, Element);
        This.modcount = L.modcount;
    size++;
      Public E-Remove (int index) {//remove element Rangecheck (index);
        To determine whether This.modcount equals L.modcount, throw an exception checkforcomodification () if sublist is used to modify the collection of external classes (which affects the Modcount method);
        E result = L.remove (Index+offset);
        This.modcount = L.modcount;
        size--;
    return result; } protected void RemoveRange (int fromindex, int toindex) {//Remove Fromindex (including) to Toindex (excluding) elements//Judgments This.modcount
        is equal to L.modcount, if a sublist is used to modify the collection of external classes (which affects the Modcount method), throw an exception checkforcomodification ();
        L.removerange (Fromindex+offset, Toindex+offset);
        This.modcount = L.modcount;
    Size = (Toindex-fromindex);
    public boolean addall (collection<? extends e> c) {//increase the elements in the collection return AddAll (size, c); public boolean addall (int index, COLLECTION&LT;? extends e> c) {//Increase the element Rangecheckforadd (index) in the collection starting from index;
        int csize = C.size ();

        if (csize==0) return false;
        Checkforcomodification ();
        L.addall (Offset+index, C);
        This.modcount = L.modcount;
        Size + = CSize;
    return true;
    The public iterator<e> iterator () {//returns the iterator return Listiterator ();
        Public listiterator<e> listiterator (final int index) {//iterator implementation, same as Front checkforcomodification ();

        Rangecheckforadd (index);

            return new listiterator<e> () {private final listiterator<e> i = L.listiterator (Index+offset);
            public Boolean Hasnext () {return Nextindex () < size;
                Public E Next () {if (Hasnext ()) return I.next ();
            else throw new nosuchelementexception (); public Boolean hasprevious () {return Previousindex () >= 0;
                Public E Previous () {if (hasprevious ()) return i.previous ();
            else throw new nosuchelementexception ();
            public int Nextindex () {return i.nextindex ()-offset;
            public int Previousindex () {return i.previousindex ()-offset;
                public void Remove () {i.remove ();
                SubList.this.modCount = L.modcount;
            size--;
            public void Set (E e) {i.set (e);
                public void Add (e) {i.add (e);
                SubList.this.modCount = L.modcount;
            size++;
    }
        }; Public list<e> sublist (int fromindex, int toindex) {//build new List, shallow copy return to new sublist<> (this, F
    Romindex, Toindex); } private void Rangecheck (int index) {//Parameter check if (Index < 0 | | | index >= size) throw new INDEXOUTOFBO
    Undsexception (outofboundsmsg (index));  private void Rangecheckforadd (int index) {//Parameter check if (Index < 0 | | index > size) throw new
    Indexoutofboundsexception (outofboundsmsg (index));
    Private String outofboundsmsg (int index) {//Exception Information Encapsulation return "index:" +index+ ", Size:" +size; }//Determine if This.modcount equals L.modcount, throw an exception private void if you use sublist to modify the collection of external classes (which affects the Modcount method) Checkforcomodification () {if (This.modcount!= l.modcount) throw new Concurrentmodificationexception (
    ); }//Implementation of the Randomaccess interface, tag interface (Marker), it has no method.
If the list subclass implements the Randomaccess interface, it means it can quickly and randomly access the stored elements. Class Randomaccesssublist<e> extends sublist<e> implements randomaccess {randomaccesssublist (AbstractLis
    t<e> list, int fromindex, int toindex) {super (list, Fromindex, toindex); Public List<e> sublist (int fromindex, int toindex) {//Build new list return randomaccesssublist<> (this, Fromindex,
    Toindex);
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.