ArrayList JDK9源码解读
来源:互联网 发布:bcg矩阵是什么意思 编辑:程序博客网 时间:2024/06/08 11:22
1、modCount = 0; 属性记录容器修改的版本号,在进行Iterator的next时,检查list是否修改,如果修改抛出ConcurrentModificationException();
public E next() {
//检查list是否修改,如果修改抛出ConcurrentModificationException();
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
2、 //通过比较对象的修改版本号进行判断是否修改了对象容器
final void checkForComodification() {
//通过比较对象的修改版本号进行判断是否修改了对象容器
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
3、遍历容器时的Iterator方法是继承了父类方法,在父类通过内部类实现Iterator接口
private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0;
/**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1;
/**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
public E next() {
checkForComodification();
try {
int i = cursor;
E next = get(i);
lastRet = i;
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
4、get(i)方法才用了Objects类新增的判断越界方法
public E get(int index) {
Objects.checkIndex(index, size);
return elementData(index);
}
5、基于数组实现,容量扩容时有如下:
默认的构造容量为空。
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
//计算扩容算法代码如下:
private int newCapacity(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);////如果原容量为4新的容量为4+2=6
//如果添加的序列大于系统扩容序列,判断原是否为空,如果空返回10或者添加序列。
if (newCapacity - minCapacity <= 0) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
return Math.max(DEFAULT_CAPACITY, minCapacity);//如果为第一个数据添加,默认扩容为10
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return minCapacity;
}
return (newCapacity - MAX_ARRAY_SIZE <= 0)
? newCapacity
: hugeCapacity(minCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE)
? Integer.MAX_VALUE
: MAX_ARRAY_SIZE;
}
- ArrayList JDK9源码解读
- HashMap JDK9源码解读
- Java--ArrayList源码解读
- ArrayList源码解读
- Java源码解读-ArrayList
- ArrayList源码解读
- ArrayList源码解读
- ArrayList源码解读
- ArrayList源码解读
- ArrayList详解,源码解读
- ArrayList源码简略解读
- ArrayList扩容源码解读
- ArrayList源码解读
- ArrayList源码解读
- JDK之ArrayList源码解读
- ArrayList底层实现源码解读
- 1.7版本 Arraylist源码解读
- jdk 1.8 arraylist源码解读
- iOS二维码扫描
- 《Python学习手册》学习笔记(11)之第11章赋值、表达式和打印(关键词:编程语言/Python/赋值/表达式/打印)
- linux-image-3.19.0-66-generic造成dkpg无法正常工作的问题
- Java学习之基础训练三-----猜字符小游戏(自定义函数,在main函数中调用)
- Good Numbers
- ArrayList JDK9源码解读
- poj1236 Network of Schools (Targan强连通图缩点)
- 1.前端之路-一个能进行2d缩放旋转的小东西
- opencv是什么
- 排序算法Day2
- [2017.10.6]作业01
- <C语言>关于函数返回值是指针,取不到值的问题研究
- 递归+Dom4j实现数据库中树(Tree)XML的生成
- 解决IE11兼容HTML5 设置