Java – sorted concurrentmodification exception

I wrote this applet to sort arrays According to my understanding, it should print 0,1,2

However, when I run this program, I receive a concurrentmodificationexception

public class Test {
    public static void main(String[] args) {
    List<Double> l1 = new ArrayList<Double>(Arrays.asList(2.,0.,1.));
    List<Double> l2 = l1.subList(0,3);
    Collections.sort(l1);
    System.out.println(l2.get(0));
    }
}

I'm really not sure the root cause of this exception

Can someone help me understand my mistakes?

Note: this problem does not exist in Java 7. It would be great if someone also said that the reason in Java 8 is not Java 7

Solution

List. The API document of sublist says:

Sorting does change the list so that ongoing iterations can lead to incorrect results The API documentation says that what happens in this case is undefined - in fact, it means throwing a concurrentmodificationexception (at least when using java 8), as shown in the code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>