For each loop | Iterator |
For each loop not allowed to delete the objects throws Concurrent modification exception. for (String text : list) { //if(text.equals("A")) //list.remove(text); System.out.println("\n After deleting The Element in array::" + text); } } | Iterator allowed to delete the objects Iterator<String> iterator = (Iterator<String>) list.iterator(); Using while loop while(iterator.hasNext()) { String element = iterator.next(); System.out.print(element + " "); if(element.equals("A")) iterator.remove(); } Using for loop for(Iterator<String> flavoursIter= aFlavours.iterator(); flavoursIter.hasNext();) { System.out.println( flavoursIter.next() ); } |
Build your Java Concepts