久久久久久久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)
主站蜘蛛池模板: 久久毛片 | 久久久久国产精品 | 国产日韩欧美精品一区二区 | 精品精品视频 | 日韩区 | 久久久久久国产精品 | 亚洲瑟瑟 | 一区二区手机在线 | 欧美精品综合在线 | 久久久爽爽爽美女图片 | 久久久久久久久久毛片 | 狠狠操狠狠操 | 亚洲一区二区三区免费在线 | 久久久国产网站 | 国产区在线 | 国产成人99久久亚洲综合精品 | 日本韩国欧美在线观看 | 久久国产精品免费 | 日韩影音| 日本成人中文字幕 | 在线观看的av | 美女久久久久久久 | 亚洲欧美综合 | 亚洲欧美日本国产 | 涩在线 | 精品美女 | 伊人春色在线观看 | 免费一级片 | 国产在线一区二 | 亚洲成av | 夜夜爽夜夜操 | 国产精品亚洲第一区在线暖暖韩国 | 欧美在线一区二区视频 | 999免费视频 | 亚洲精美视频 | 91色站 | 日韩三级电影一区二区 | 九色视频网站 | 国产午夜视频 | 少妇无套高潮一二三区 | 欧美日日 |