1樓:
1. 程式裡沒有用iterator的remove(),而是用list的remove(int)。 ArrayList的remove(int)的實現:
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index, numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
注意,size減了1.
public boolean hasNext() {
return cursor != size;
當前cursor位置不變,而size卻比剛才少了1,所以hasNext == size了。返回true。迴圈就退出了。整個流程根本不會拋異常。
3. 如果你刪掉的是第乙個或者第三個元素,那就會報異常了,因為next方法裡呼叫 checkForComodification(),如果modCount != expectedModCount,會拋異常。
個人覺得,這是JDK的乙個bug。
Java中ArrayList最大容量為什麼是Integer MAX VALUE 8
徐斐然 為什麼減8已經有人回答了,但是事實上ArrayList的最大容量不是Integer.MAX VALUE 8,而是 Integer.MAX VALUE。原始碼如下 The maximum size of array to allocate.Some VMs reserve some heade...
在Oracle VM VirtualBox中安裝增強功能時,有錯誤 失敗 。求高人指點。?
補充 下面的內容是安裝日誌 make KBUILD VERBOSE 1 CONFIG MODULE SIG C lib modules 2.6.18 8.10WS build SUBDIRS tmp vbox.0 SRCROOT tmp vbox.0 modules test e include l...
在Java的NIO程式設計中,在ACCEPT事件監聽處理中,使用阻塞和非阻塞對效率影響大嗎?
觸發器 是一樣的,因為selecter的select 方法在沒有accept事件的時候也會阻塞。另外只處理accept事件又不需要處理讀寫資料,就沒有其他阻塞了。 程羅 乙個執行緒a阻塞在accept上,另乙個執行緒b阻塞在selector的select上。每當到來乙個連線,a都要呼叫selecto...