我在学习LinkedList的时候,看了一下源码,有几处看的不是很懂,希望大神解释一二:
1、这代码中根据下标获取节点的这个是怎么判断的?什么逻辑?
/**
* Returns the indexed entry.
*/
private Entry<E> entry(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
Entry<E> e = header;
if (index < (size >> 1)) {
for (int i = 0; i <= index; i++)
e = e.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
} return e;
}2、modCount这个变量是做什么的?怎么没找到在哪定义的?
1、这代码中根据下标获取节点的这个是怎么判断的?什么逻辑?
/**
* Returns the indexed entry.
*/
private Entry<E> entry(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
Entry<E> e = header;
if (index < (size >> 1)) {
for (int i = 0; i <= index; i++)
e = e.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
} return e;
}2、modCount这个变量是做什么的?怎么没找到在哪定义的?
(PS:我没阅读过源码,确实我有混分的嫌疑)为了拿分大致的搜索看了一下(拿分不容易)
modCount,你发现这个变量在所有有增删操作的地方都是++ 然后赋值给了内部的迭代器,应该是为了防止在使用迭代期的时候linkedlist 又进行了增删的操作 checkForComodification()这个函数就是检测用的。仓促之中,如有不对,还请指正。