Code with exception
public static void main(String[] args) { ListCode without exception using IteratormyList = new ArrayList (); myList.add("1"); myList.add("2"); myList.add("3"); myList.add("4"); myList.add("5"); Iterator it = myList.iterator(); while(it.hasNext()){ String value = it.next(); if(value.equals("3")){ myList.remove("3"); } } System.out.println("List Size:"+myList.size()); }
public static void main(String[] args) { ListReference: http://www.javacodegeeks.com/2011/05/avoid-concurrentmodificationexception.htmlmyList = new ArrayList (); myList.add("1"); myList.add("2"); myList.add("3"); myList.add("4"); myList.add("5"); Iterator it = myList.iterator(); while(it.hasNext()){ String value = it.next(); if(value.equals("3")){ it.remove(); } } System.out.println("List Size:"+myList.size()); }
Comments
Post a Comment