久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么一個循環會拋出 ConcurrentModificationExceptio

Why does one loop throw a ConcurrentModificationException, while the other doesn#39;t?(為什么一個循環會拋出 ConcurrentModificationException,而另一個不會?)
本文介紹了為什么一個循環會拋出 ConcurrentModificationException,而另一個不會?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在編寫旅行推銷員程序時遇到了這個問題.對于內部循環,我嘗試了

I've run into this while writing a Traveling Salesman program. For an inner loop, I tried a

for(Point x:ArrayList<Point>) {
// modify the iterator
}

但是當向該列表添加另一個點時會引發 ConcurrentModicationException.

but when adding another point to that list resulted in a ConcurrentModicationException being thrown.

但是,當我將循環更改為

However, when I changed the loop to

for(int x=0; x<ArrayList<Point>.size(); x++) {
// modify the array
}

循環運行良好,沒有拋出異常.

the loop ran fine without throwing an exception.

都是for循環,為什么一個會拋出異常而另一個不會呢?

Both a for loops, so why does one throw an exception while the other does not?

推薦答案

正如其他人解釋的那樣,迭代器檢測到對底層集合的修改,這是一件好事,因為它可能會導致意外行為.

As others explained, the iterator detects modifications to the underlying collection, and that is a good thing since it is likely to cause unexpected behaviour.

想象一下這個修改集合的無迭代器代碼:

Imagine this iterator-free code which modifies the collection:

for (int x = 0; list.size(); x++)
{
  obj = list.get(x);
  if (obj.isExpired())
  {
    list.remove(obj);
    // Oops! list.get(x) now points to some other object so if I 
    // increase x again before checking that object I will have 
    // skipped one item in the list
  }
}

這篇關于為什么一個循環會拋出 ConcurrentModificationException,而另一個不會?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 91操操操 | 麻豆视频在线免费看 | 野狼在线社区2017入口 | 国产羞羞视频在线观看 | 日韩av免费在线观看 | 亚洲精品乱码久久久久久蜜桃91 | 国产乱码精品一区二区三区中文 | 日本视频在线播放 | 国产高清精品在线 | 国产欧美日韩精品在线观看 | 国产精品久久久久国产a级 欧美日本韩国一区二区 | 日韩av高清 | 一区二区免费在线观看 | 国产欧美一区二区精品久导航 | 亚洲欧美日韩在线不卡 | 欧美在线一区二区三区四区 | 久久国产欧美日韩精品 | 蜜桃视频在线观看免费视频网站www | 日本淫视频 | 亚洲天堂av在线 | 精品一二三区 | 国产精品久久久久久婷婷天堂 | 91看片免费版 | 91视视频在线观看入口直接观看 | 91视频国产一区 | 成人h视频| 亚洲国产精品网站 | 免费看淫片| 日本综合在线观看 | 极品粉嫩国产48尤物在线播放 | 九九福利| 亚洲福利 | 欧美色综合天天久久综合精品 | 麻豆av网| 蜜桃av人人夜夜澡人人爽 | 午夜影视在线观看 | 高清人人天天夜夜曰狠狠狠狠 | 国产精品欧美一区二区三区不卡 | 7777在线视频 | 91精品国产自产精品男人的天堂 | 精品国产乱码久久久久久丨区2区 |