AbstractList抽象类
注:
由于本人未系统学习异常,本文异常名不应作为标准。
功能:
实现List中骨干方法,减少实现List接口时的工作。
内容:
继承的抽象类:AbstractCollection<E>
实现的接口:List<E>
直接子类:AbstractSequentiaList ArrayList Vector
protected transient int modCount = 0;
受保护的成员变量,为迭代器提供快速失败行为,若集合进行了修改,则+1。不在序列化范围内。初始化为0。
protected AbstractList()
受保护的构造方法,可以被子类调用。
public boolean add(E e)
方法返回boolean值(为true,否则抛出异常)。指定者为List中的add 方法,方法重写AbstractCollection中的add() 方法,方法覆盖AbstractCollection中的add() 方法。add(size(), e);调用重载的add() 方法,将size() 的返回值作为第一个参数。return true;方法返回true。
abstract public E get(int index);
抽象方法返回泛型E的引用。指定者为List中的get方法。
public E set(int index, E element)
方法返回泛型E的引用。指定者为List中的set方法。throw new UnsupportedOperationException();方法抛出无操作异常。
public void add(int index, E element)
方法返回值为空。指定者为list中的add 方法,方法重载add(E e)方法。throw new UnsupportedOperationException();方法抛出无操作异常。
public E remove(int index)
方法返回泛型E的引用。指定者为List中的remove方法。throw new UnsupportedOperationException();方法抛出无操作异常。
public int indexOf(Object o)
方法返回int值。指定者为list中的indexOf方法。ListIterator<E> it = listIterator();首先创建迭代器,if (o==null)如果传入参数为null,while (it.hasNext())遍历集合元素,if (it.next()==null)判断下一元素是否为null,return it.previousIndex();若是,则返回此元素的位置。else当传入参数不为null,while (it.hasNext())遍历集合元素,if (o.equals(it.next()))判断参数和下一个元素是否相同,return it.previousIndex();若是,则返回此元素的位置。return -1;当没有找到相同元素时,返回-1。
public int lastIndexOf(Object o)
方法返回int值。指定者为List中的lastIndexOf方法。ListIterator<E> it = listIterator(size());首先创建迭代器,if (o==null)判断传入参数是否为null,while (it.hasPrevious())若是,倒序遍历集合,if (it.previous()==null)判断上一个元素是否为null,return it.nextIndex();若是,返回该元素的位置。else当传入参数不为null,while (it.hasPrevious())倒序遍历集合,if (o.equals(it.previous()))判断参数和上一个元素是否相同,return it.nextIndex();若是,返回该元素的位置。return -1;当没有找到相同元素时,返回-1。
public void clear()
方法返回值为空。指定者为List中的clear方法,方法覆盖AbstractCollection中的clear() 方法。removeRange(0, size());调用本类的removeRange()方法进行删除,将0和size() 方法的返回值作为参数传递。
public boolean addAll(int index, Collection<? extends E> c)
方法返回boolean值。指定者为List中的addAll方法。rangeCheckForAdd(index);调用本类的rangeCheckForAdd()方法对参数指定位置进行检查。boolean modified = false;设置变量并将其置为false。for (E e : c) 遍历参数集合,add(index++, e);将参数集合的元素添加到原集合指定位置处,modified = true;将modified置为true。return modified;遍历结束后,返回modified的值。(传入集合应为原集合的协变类型)
public Iterator<E> iterator()
方法返回Iterator类型对象的引用。指定者为List中的iterator方法,指定者为AbstractCollection中的iterator方法。return new Itr();创建一个内部类Itr的对象,并将它的引用作为返回值返回。
public ListIterator<E> listIterator()
方法返回ListIterator类型对象的引用。指定者为List中的ListIterator方法。return listIterator(0);调用重载方法,将0作为参数,并将其返回值作为返回值返回。
public ListIterator<E> listIterator(final int index)
方法返回ListIterator类型对象的引用。指定者为List中的ListIterator方法,方法重载listIterator() 方法。rangeCheckForAdd(index);对指定位置进行检查。return new ListItr(index);将参数作为构造方法的参数创建ListItr对象,并将其引用作为返回值返回。(这里参数index用final修饰,不可改变。(个人认为,标志意义大于实际意义))
public List<E> subList(int fromIndex, int toIndex)
方法返回List类型对象的引用。指定者为List中的subList方法。return (this instanceof RandomAccess ?判断原集合是否实现了RandomAccess接口。new RandomAccessSubList<>(this, fromIndex, toIndex) :若是,则返回RandomAccessSubList对象的引用。(将原集合截取后得到的)new SubList<>(this, fromIndex, toIndex));否则返回SubList对象的引用。(将原集合截取后得到的)(SubList继承AbstractCollection,RandomAccess继承SubList)
public boolean equals(Object o)
方法返回boolean值。指定者为List中的equals方法,指定者为AbstractCollection中的equals方法,覆盖Object中的equals方法。if (o == this)判断是否为本对象的引用, return true;若是,方法返回true。if (!(o instanceof List))判断传入对象是否实现List接口, return false;若否,返回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)))若原集合元素为null,判断比较集合的元素是否为null,若是o2==null为true,if为false,不成立;反之,成立,o1,o2不同。若原集合不为null,执行equals方法,同理,两元素相同,if不成立,反之成立。return false;若if成立,方法返回false。return !(e1.hasNext() || e2.hasNext());当遍历结束后,判断两集合中是否存在集合仍有元素没被遍历。若有,方法返回false,若两者都没有下一个元素,则返回true。
public int hashCode()
方法返回int值。指定者为List中的hashCode方法,覆盖了Object中的hashCode方法。int hashCode = 1;定义变量并赋初值为1。for (E e : this)遍历本集合。hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());当元素为null时,hashCode自乘31,当元素不为null时,hashCode自乘31后加上元素的hashCode。return hashCode;遍历结束后返回hashCode。(hashCode的计算方式)
protected void removeRange(int fromIndex, int toIndex)
受保护方法返回值为空。ListIterator<E> it = listIterator(fromIndex);调用本类listIterator方法创建迭代器,从fromIndex开始。for (int i=0, n=toIndex-fromIndex; i<n; i++)循环toIndex-fromIndex次。it.next();访问下一个元素。it.remove();移除该元素。
private void rangeCheckForAdd(int index)
私有方法返回值为空。if (index < 0 || index > size())判断参数是否在0~size方法返回值之间。throw new IndexOutOfBoundsException(outOfBoundsMsg(index));若不在,抛出越界异常。
private String outOfBoundsMsg(int index)
私有方法返回String类型对象的引用。return "Index: "+index+", Size: "+size();返回错误信息。
Itr私有内部类
功能:
作为AbstractList抽象类的迭代器。
内容:
实现的接口:Iterator<E>
int cursor = 0;
定义变量存储当前位置。
int lastRet = -1;
定义变量存储上一位置。
int expectedModCount = modCount;
定义变量用于存储期望modCount。用于判断集合是否被意外修改。
默认构造方法。
public boolean hasNext()
方法返回boolean值。指定者为Iterator中的hasNext方法。return cursor != size();返回当前位置是否不为size方法(外部类的)的返回值。
public E next()
方法返回泛型类型的对象。指定者为Iterator中的next方法。checkForComodification();调用本类checkForComodification方法检查集合是否意外修改。(不再赘述)try捕捉后面代码的异常,int i = cursor;定义变量赋值为当前位置,E next = get(i);获取当前位置元素,将其引用暂存。lastRet = i;将上一位置置为i,cursor = i + 1;将当前位置置为i+1。return next;返回获取的元素。catch (IndexOutOfBoundsException e)捕捉越界异常(发生在get方法),checkForComodification();检查集合,throw new NoSuchElementException();若没有意外修改,抛出没找到元素异常。
public void remove()
方法返回值为空。指定者为Iterator中的remove方法。if (lastRet < 0)判断上一个位置是否小于0,(是否存在)throw new IllegalStateException();若是,抛出位置非法异常。checkForComodification();检查集合,try捕捉后面代码的异常,AbstractList.this.remove(lastRet);调用外部类的remove方法,删除上一个元素。if (lastRet < cursor)判断当前位置是否大于前一位置,cursor--;若是,当前位置-1。lastRet = -1;上一位置赋为-1,因为被删除。(Reset to -1 if this element is deleted by a callto remove)expectedModCount = modCount;将期望modCount同步。catch (IndexOutOfBoundsException e)捕获越界异常。(外部类remove()方法抛出)throw new ConcurrentModificationException();抛出并发修改异常。(不能连续执行删除)
final void checkForComodification()
方法返回值为空。if (modCount != expectedModCount)判断预期与原modCount是否一致,throw new ConcurrentModificationException();若不一致,抛出并发修改异常。
ListItr私有内部类
功能:
作为AbstractList抽象类的迭代器。
内容:
继承的类:Itr内部类
实现的接口:ListIterator<E>
ListItr(int index)
Default类型构造方法。cursor = index;将传入参数赋值给当前指针。
public boolean hasPrevious()
方法返回boolean值。指定者为ListIterator中的hasPrevious方法。return cursor != 0;判断当前位置是否为0,若不是,返回true。
public E previous()
方法返回泛型E的引用。指定者为ListIterator中的previous方法。checkForComodification();调用父类的方法检查是否意外修改过。try捕捉后面代码的异常。int i = cursor - 1;定义变量并赋值为当前位置-1。E previous = get(i);获得get方法(外部类)返回的引用。lastRet = cursor = i;将当前位置和上一个位置置为i。(不置为i+1的原因可能是考虑到删除,个人认为lastRet只是为删除、设置操作设置的)return previous;返回获得的引用。catch (IndexOutOfBoundsException e)捕获越界异常。(get方法处)checkForComodification();检查是否意外更改。 throw new NoSuchElementException();若不是意外修改,抛出未找到元素异常。
public int nextIndex()
方法返回int值。指定者为ListIterator的nextIndex方法。return cursor;返回当前位置。
public int previousIndex()
方法返回int值。指定者为ListIterator的previousIndex方法。return cursor-1;返回当前位置-1。
public void set(E e)
方法返回值为空。指定者为ListIterator的set方法。if (lastRet < 0)判断上一个位置是否小于0,(是否存在)throw new IllegalStateException();若是,抛出位置非法异常。checkForComodification();检查集合,try捕捉后面代码的异常,AbstractList.this.set(lastRet, e);调外部类set方法,为上一位置进行设置。expectedModCount = modCount;将期望modcount同步。catch (IndexOutOfBoundsException ex)捕获越界异常(set方法处),throw new ConcurrentModificationException();抛出并发修改错误。
public void add(E e)
方法返回值为空。指定者为ListIterator中的add方法。checkForComodification();检查集合,try 捕捉后面代码的异常。int i = cursor;定义变量赋值为当前位置。 AbstractList.this.add(i, e);调用父类add方法当前位置处添加元素e。lastRet = -1;将上一位置置为-1。(不能在add方法后调用remove方法)cursor = i + 1;当前位置为i+1(原位置+1)。expectedModCount = modCount;将期望modcount同步。catch (IndexOutOfBoundsException ex)捕获越界异常。throw new ConcurrentModificationException();抛出并发修改异常。
SubList类(与AbstractList类同文件)
功能:
实现截取AbstractList的部分。
内容:
继承的类:AbstractList类。
private final AbstractList<E> l;
私有的常量用于存储原数组的引用
private final int offset;
私有的常量用于存储开始位置
private int size;
私有的变量用于存储截取的集合的长度(可通过方法操作改变)
SubList(AbstractList<E> list, int fromIndex, int toIndex)
Default类型构造方法。if (fromIndex < 0)判断传入的开始位置是否小于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;将原集合的引用赋值给l。offset = fromIndex;将开始位置赋值给offset。size = toIndex - fromIndex;将开始位置到末位置的长度赋值给size。this.modCount = l.modCount;将原集合的modcount赋值给父类的modCount。
public E set(int index, E element)
方法返回泛型E的引用。方法覆盖AbstractList中的set方法。rangeCheck(index);检查index是否在合法范围。checkForComodification();检查是否意外修改过。return l.set(index+offset, element);调用原数组的set方法 。并作为返回值返回。
public E get(int index)
方法返回泛型E的引用。方法覆盖AbstractList中的get方法。rangeCheck(index);检查参数是否合法。checkForComodification();检查是否意外修改过。return l.get(index+offset);调用原数组get方法。并作为返回值返回。
public int size()
方法返回int值。指定者为List中的size方法。方法覆盖AbstractList中的size方法。checkForComodification();检查是否意外修改过。return size;返回成员变量size。
public void add(int index, E element)
方法返回值为空。方法覆盖AbstractList中的add方法。rangeCheckForAdd(index);检查参数是否合法。(包括长度值)checkForComodification();检查是否意外修改过。l.add(index+offset, element);调用原数组add方法(这里的插入位置为本数组中的位置在原数组中对应的位置)。this.modCount = l.modCount;将modCount同步。size++;长度+1。
public E remove(int index)
方法返回泛型E的引用。方法覆盖AbstractList中的remove方法。rangeCheck(index);检查参数是否合法。checkForComodification();检查是否意外修改过。E result = l.remove(index+offset);调用原数组的remove方法。并将返回值储存。this.modCount = l.modCount;将modcount同步。size--;长度-1。return result;返回保存的值。
protected void removeRange(int fromIndex, int toIndex)
受保护方法返回值为空。方法覆盖AbstractList中的removeRange方法。checkForComodification();检查是否意外修改过。l.removeRange(fromIndex+offset, toIndex+offset);调用原数组removeRange方法。this.modCount = l.modCount;同步modCount。size -= (toIndex-fromIndex);令长度减去移除的元素长度。
public boolean addAll(Collection<? extends E> c)
方法返回boolean值。指定者为List中的addAll方法。方法覆盖AbstractList中的addAll方法。return addAll(size, c);调用重载方法addAll。并将返回值作为返回值返回。(默认添加位置在截取的集合末尾)
public boolean addAll(int index, Collection<? extends E> c)
方法返回boolean值。方法覆盖AbstractList中的addAll方法。rangeCheckForAdd(index);检查参数是否合法。int cSize = c.size();定义变量存储c的长度。if (cSize==0)判断c的长度是否为0。return false;若为0返回false。checkForComodification();检查是否意外修改过。l.addAll(offset+index, c);调用原数组addAll方法。this.modCount = l.modCount;同步modcount。size += cSize;令长度加上c的长度。return true;方法返回true。
public Iterator<E> iterator()
方法返回迭代器。方法覆盖AbstractList中的Iterator方法。return listIterator();方法
public ListIterator<E> listIterator(final int index)
方法返回List迭代器。方法覆盖AbstractList中的listIterator方法。checkForComodification();检查是否意外修改过。rangeCheckForAdd(index);检查参数是否合法。return new ListIterator<E>() {返回匿名内部类。实现ListIterator接口。
private final ListIterator<E> i = l.listIterator(index+offset);
私有常量。原集合的迭代器。从截取起始位置与参数之和开始。
public boolean hasNext()
方法返回boolean值。return nextIndex() < size;判断下一位置是否小于size(成员变量,因为截取集合含头不含尾,所以不是等于)。
public E next()
方法返回泛型E的引用。if (hasNext())判断是否存在下一个元素。 return i.next();若存在下一个元素,调用原集合迭代器的next方法,并返回。else否则,throw new NoSuchElementException();抛出未找到元素异常。
public boolean hasPrevious()
方法返回boolean值。return previousIndex() >= 0;判断previousIndex方法返回值是否大于或等于0,并返回判断结果。
public E previous()
方法返回泛型E的引用。if (hasPrevious())判断是否有前一个元素。return i.previous();若有,则调用原集合迭代器的取前一个元素方法,并返回。else否则,throw new NoSuchElementException();抛出未找到元素异常。
public int nextIndex()
方法返回int值。return i.nextIndex() - offset;调用原集合迭代器的nextIndex方法,并减去截取集合的开始位置,作为返回值返回。
public int previousIndex()
方法返回int值。return i.previousIndex() - offset;调用原集合迭代器的previousIndex方法,并减去截取集合的开始位置,作为返回值返回。
public void remove()
方法返回值为空。i.remove();调用原集合迭代器的remove方法,SubList.this.modCount = l.modCount;将SubList类(外部类)的modCount同步。size--;将长度-1。
public void set(E e)
方法返回值为空。i.set(e);调用原集合迭代器的set方法。
public void add(E e)
方法返回值为空。i.add(e);调用原集合迭代器的add方法。SubList.this.modCount = l.modCount;将modcount同步。size++;令长度+1。
匿名内部类结束。
public List<E> subList(int fromIndex, int toIndex)
方法返回List类型对象的引用。方法覆盖AbstractList中的subList方法。return new SubList<>(this, fromIndex, toIndex);返回新的SubList对象的引用。
private void rangeCheck(int index)
方法返回值为空。if (index < 0 || index >= size)判断参数是否不在0和size之间,(包括size),throw new IndexOutOfBoundsException(outOfBoundsMsg(index));若是,抛出索引越界异常。
private void rangeCheckForAdd(int index)
方法返回值为空。if (index < 0 || index > size)判断参数是否不在0和size之间,(不包括size),throw new IndexOutOfBoundsException(outOfBoundsMsg(index));若是,抛出索引越界异常。
private String outOfBoundsMsg(int index)
方法返回String类型引用。return "Index: "+index+", Size: "+size;返回字符串。
private void checkForComodification()
方法返回值为空。if (this.modCount != l.modCount)判断本类的modCount和原集合modCount是否一致。throw new ConcurrentModificationException();若不一致,抛出并发修改异常。
RandomAccessSubList类(与AbstractList类同文件)
功能:
实现截取AbstractList的部分。(实现RandomAccess接口)
内容:
继承的类:SubList。
实现的接口:RandomAccess。
RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex)
Default修饰的构造方法。super(list, fromIndex, toIndex);调用SubList的构造方法。
public List<E> subList(int fromIndex, int toIndex)
方法返回List类型的引用。方法覆盖SubList中的subList方法。return new RandomAccessSubList<>(this, fromIndex, toIndex);返回新的RandomAccessSubList对象的引用。