java - Weird behaviour of ArrayList -
this question has answer here:
i have following code -
import java.util.arraylist; import java.util.list; public class arraylisttest { public static void main(string[] args) { list<string> list = new arraylist<>(); list.add("1a"); list.add("2b"); (string s : list) { list.remove(s); } system.out.println("removed"); system.out.println(list); } }
if run program, expect should throw exception output "2b". if running fine why not removing last object.
again if add more element in list thorwing exception java.util.concurrentmodificationexception expected.
my question -
- why not removing elements list if have 2 elements in list ??
- why java.util.concurrentmodificationexception exception occur when have more elements ?? tried lot of time 2 elements.
i using java 8.
thanks in advance.
actually, when removing 1a list, size getting reduced, , have 1 element in list, loop not execute second time, there resulting in keeping second element.
Comments
Post a Comment