用java源代码学数据结构<三>:ArrayList 详解(八)

2014-11-24 08:51:45 · 作者: · 浏览: 4
cationException();
}
};
}
public List subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, offset, fromIndex, toIndex);
}
private void rangeCheck(int index) {
if (index < 0 || index >= this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void rangeCheckForAdd(int index) {
if (index < 0 || index > this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+this.size;
}
private void checkForComodification() {
if (ArrayList.this.modCount != this.modCount)
throw new ConcurrentModificationException();
}
}
}
个人体会:
1.Vector是线程安全的,同步的,ArrayList是不安全,不同步的。要想使得ArrayList同步,可以使用List list = Collections.synchronizedList(new ArrayList(...))来进行封装
2.通过比较vector和arraylist的private void grow(int minCapacity)方法,可以发现vector每次增加原来容量的一倍,而ArrayList每次增加原来容量的一半
3.ArrayList和Vector类中都包含两个迭代器:普通迭代器Itr,链表迭代器ListItr,其实它们的内部功能都是使用集合的内部属性或方法来实现的,只是函数名的不同以及功能的不同而已