Re: [蟲?]ConcurrentModificationException
ArrayList is fail-fast. In the scenario where one thread is acessing
to the list whilst the other is trying to do modification,
ConcurrentModificationException will be thrown. Applying
Collection.synchronizedXXXX()
or concurrent package can solve this problem.
http://www.ibm.com/developerworks/java/library/j-jtp07233/index.html
※ 引述《stupid5566 (屎丟比5566)》之銘言:
: public class ComicService {
: private Collection<ComicModel> models = new ArrayList<ComicModel>();
: public void delete(String isbnCode){
: for (ComicModel model : models){
: if(model.getIsbnCode().equals(isbnCode)){
: models.remove(model);
: }
: }
: }
: }
: 主程式:
: public class ComicApp {
: public static void main(String[] args) {
: ComicService comicService = new ComicService();
: comicService.delete("003");
: }
: }
: 結果跳出 java.util.ConcurrentModificationException 錯誤
: Model裡面就是簡單的getIsbnCode() setIsbnCode()
: 學長寫得一模一樣 但是可以成功刪除
: 出了什麼問題嗎
: 啾啾啾啾啾啾我!!!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.168.73.251
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):