java source code - AbstractList

AbstractList

  AbstractList implement the List interface is an abstract class , abstract class and relationship AbstractList List interface similar to the relationship AbstractCollection Collection abstract classes and interfaces.

  AbstractList and AbstractCollection, just as some of the default method is achieved by simplifying our efforts to prepare a list of the List interface class required to pay.

Implement a list of classes to keep in mind:

  1) To achieve an unmodifiable set subclassing the class, and implements get (int), size () method;
  2) To implement a modifiable collection, you must also override the set (int, E) method, the default method throws an exception. If the set size is dynamically adjusted, it must also be rewritten add (int, E), remove (int) method.

method

 public boolean add(E e){}

public  Boolean the Add (E E) { 
        the Add (size (), E); // add by invoking add (int, E) method 
        return  to true ; 
    }

// This method is not specific implementation, when you call it, throw an exception.

public void add(int index, E element){ throw new UnsupportedOperationException(); }
public E set(int index, E element) { throw new UnsupportedOperationException(); }
public E remove(int index) {throw new UnsupportedOperationException();}

E public abstract GET (int index); // no realization

// Search Operations search operation
// By iterator to iterate over the contents 
public  int the indexOf (Object O) { 
        the ListIterator <E> = IT listIterator ();
         IF (O == null ) { // If the argument is null 
            the while (it.hasNext ())
                 IF (it.next () == null ) // find the content is empty 
                    return it.previousIndex (); // returns 
        } the else {
             the while (it.hasNext ())
                 IF (o.equals (it.next () ))
                     return it.previousIndex (); 
        } 
        return -1;
    }
public int
lastIndexOf(Object o) {
    ListIterator<E> it = listIterator(size());
if (o==null) {
while (it.hasPrevious())
if (it.previous()==null)
return it.nextIndex();
} else {
while (it.hasPrevious())
if (o.equals(it.previous()))
return it.nextIndex();
}
return -1;
}

  // Bulk Operations Bulk operations

// set Empty 
public
void Clear () { removeRange, ( 0 , size ()); // call removeRange, () method removes }
// 参数是删除的范围。
 protected void removeRange(int fromIndex, int toIndex) {
        ListIterator<E> it = listIterator(fromIndex);
        for (int i=0, n=toIndex-fromIndex; i<n; i++) {
            it.next();
            it.remove();
        }
    }
// add the collection in the collection 
public Boolean the addAll ( int index, Collection <? The extends E> C) {
       rangeCheckForAdd (index);
        Boolean Modified = to false ;
        for (E E: C) { // Fore ACH cycle add
             the Add (index ++ , E);
            Modi = to true ; 
        } 
        return MOD}

  // Iterators iterator

Two iterators implementation class

  AbstractList provides implementation class two iterator, the default implementation of the iterator interface to achieve the traversal of the elements, they are Itr , and its subclasses  ListItr, respectively, to find out.

Itr class that implements the Iterator interface rewrite the Next () and Remove () method

Private  class Itr the implements the Iterator <E> {
     // cursor 
    int Cursor;
     // latest iteration of element positions, each use default set to -1 
    int lastRet;
     // number of times the container is modified records, illustrate the concurrency values are not equal operation 
    int expectedModCount = ModCount; 
  // if there is a next element, if the cursor is not equal to the set size. Equal to the end of the collection, then there is no cursor next element 
    public  Boolean the hasNext () {
         return Cursor =! Size (); 
    } 

public E Next () {
     // detecting whether concurrent 
    checkForComodification ();
     the try {
         int I = Cursor;
        // Get the cursor position corresponding to the elements of the container 
        E = Next GET (I);
         // index of the recording element obtained 
        lastRet = I;
         // Get the next index element 
        Cursor = I +. 1 ;
         return Next; 
    } the catch ( var3 an IndexOutOfBoundsException) {
         checkForComodification ();
         the throw  new new NoSuchElementException (); 
    } 
}
void public Remove () { 
    // read element can not remove, given
  IF (lastRet <0)
  the throw new new IllegalStateException ();
  checkForComodification ();

  the try {
  AbstractList.this.remove (lastRet);
  IF (lastRet <Cursor) after // delete, set one less element, satisfies the condition cursor element from a retracted Save
  cursor--;
  lastRet = -1;
  expectedModCount = ModCount ;
  } the catch (an IndexOutOfBoundsException E) {
  the throw new new a ConcurrentModificationException ();
  }
  }
  // concurrency exception
  Final void checkForComodification () {
  IF (modCount != expectedModCount)
  throw new ConcurrentModificationException();
  }
}


ListItr is Itr child class in Itr enhance the operation of elements based on the specified index assignment more, and a method of reading forward, the Add and set.

Private  class ListItr the extends Itr the implements the ListIterator <E> { 
        ListItr ( int index) { 
            Cursor = //// sets the cursor to the specified value; index 
        } 
     //// cursor is not the first, then the front element has a 
        public  Boolean hasPrevious () {
             return cursor = 0! ; 
        } 

        public E previous () { 
            checkForComodification (); 
            the try {
                 int I = cursor -. 1 ; 
         // get previous cursor element E previous
= GET (I);
         // the last cursor position and a forward operation are lastRet
= Cursor = I; return Previous; } the catch (an IndexOutOfBoundsException E) { checkForComodification (); the throw new new NoSuchElementException (); } } public int nextIndex () { return Cursor ; } public int previousIndex () { return Cursor-. 1 ; } public void SET (E E) { IF (lastRet <0) throw new IllegalStateException(); checkForComodification(); try { AbstractList.this.set(lastRet, e); expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(E e) { checkForComodification(); try { int i = cursor; AbstractList.this.add(i, e); lastRet = -1; cursor = i + 1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } }

    AbstractList class provides two methods, the interface returns the object type of each implement:

public Iterator<E> iterator() {
    return new Itr();
}
public ListIterator<E> listIterator() {
    return listIterator(0);
}
public ListIterator<E> listIterator(final int index) {
    rangeCheckForAdd(index);

    return new ListItr(index);
}

Two subclasses

  AbstractList provides two subclasses, can be used for slicing sequence of sets , these two classes are SubList and RandomAccessSubList .

  1. SubList implementations and internal AbstractList very similar, is nothing more than passing two variables, return the specified fromIndex (inclusive) and the view, and end positions of the list of acquaintance portion between toIndex taken to set.

  2. RandomAccessSubList is a subclass SubList internal direct superclass inherit only implements RandomAccess interface.

   Not the same, RandomAccessSubList implements an interface RandomAccess, open and found empty, without any implementation.

   Its role is used to identify a class supports random access (random access, as opposed to "in order to access"). A support random access are significantly more efficient algorithms can be used. For example traversal, to achieve the set using the get RandomAccess interface () iterate faster than using an iterator words.

 

Thanks: https: //blog.csdn.net/u013164931/article/details/79640715

Guess you like

Origin www.cnblogs.com/FondWang/p/11922340.html