Java 7源码分析第8篇 - List集合基于数组的实现(二)

2014-11-24 07:20:09 · 作者: · 浏览: 6
} public static void test(ArrayList aList){ aList.add("a"); aList.add("b"); aList.add("d"); //aList.remove(2);// 抛出异常ConcurrentModificationException ListIterator it=aList.listIterator(); while(it.hasNext()){ System.out.println(it.next()); } } }

程序运行正常,因为生成的it对象只在test()方法作用范围内有效,所以在main()方法中完全可以调用ArrayList()中的方法为集合添加元素。

其实AbstractList抽象类中实现的方法的主要就依赖于Iterator和ListIterator类,如:

(1)public int indexOf(Object o)

(2)public int lastIndexOf(Object o)

(3)public void clear()

(4)public boolean addAll(int index, Collection c)

(5)protected void removeRange(int fromIndex, int toIndex) {

有兴趣的读者可以去查看源代码。

除此之外,还有一个重要的方法,方法的源代码如下:

public List
   
     subList(int fromIndex, int toIndex) {
        return (this instanceof RandomAccess  
                new RandomAccessSubList<>(this, fromIndex, toIndex) :
                new SubList<>(this, fromIndex, toIndex));
}
   

方法的功能就是要获取部分List集合,这个方法还涉及到了几个类,类的框架图如下。

在AbstractList.java源文件中除了有AbstractList抽象类中有许多具体的实现,而且还有许多的非public类,类的框架图如下:

\

SubList和RandomAccessSubList类实现的主要功能就是实现对数组中部分连续数据的操作。SubList类的源代码如下:

class SubList
   
     extends AbstractList
    
      { private final AbstractList
     
       l; private final int offset; private int size; 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; this.modCount = l.modCount; } public E set(int index, E element) { rangeCheck(index); checkForComodification(); return l.set(index+offset, element); } public E get(int index) { rangeCheck(index); checkForComodification(); return l.get(index+offset); } public int size() { checkForComodification(); return size; } public void add(int index, E element) { rangeCheckForAdd(index); checkForComodification(); l.add(index+offset, element); this.modCount = l.modCount; size++; } public E remove(int index) { rangeCheck(index); checkForComodification(); E result = l.remove(index+offset); this.modCount = l.modCount; size--; return result; } protected void removeRange(int fromIndex, int toIndex) { checkForComodification(); l.removeRange(fromIndex+offset, toIndex+offset); this.modCount = l.modCount; size -= (toIndex-fromIndex); } public boolean addAll(Collection
        c) { return addAll(size, c); } public boolean addAll(int index, Collection
        c) { rangeCheckForAdd(index); int cSize = c.size(); if (cSize==0) return false; checkForComodification(); l.addAll(offset+index, c); this.modCount = l.modCount; size += cSize; return true; } public List
       
         subList(int fromIndex, int toIndex) { return new SubList<>(this, fromIndex, toIndex); } private void rangeCheck(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private void rangeCheckForAdd(int index) {// if (index < 0 || index > size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+size; } private void checkForComodification() { if (this.modCount != l.modCount) throw new ConcurrentModificationException(); } }
       
      
     
    
   

可以看到,这个类中首先定义了的2个属性和1个实例,私有并且都被final修饰,也就是说,这个类的实例一旦生成,也就不可改变了。

AbstractList
   
     sub = new ArrayList
    
     (); sub.add("a"); sub.add("b"); sub.add("c"); sub.add("d"); List l = sub.subList(1, 3); //s