Sublist in Java

Source: Internet
Author: User

Package test;

Import java.util.ArrayList;
Import java.util.List;


Structural modification of parentlist, access to sublist throws an exception

public class Testsublist {
public static void Main (string[] args) {
list<string> parentlist = new arraylist<string> ();
for (int i = 0;i<10;i++) {
Parentlist.add (string.valueof (i));
}

list<string> sublist = parentlist.sublist (0,3);
System.out.println (sublist);
Sublist.clear ();

Parentlist.add ("undef");
System.out.println (sublist);

}
}

This piece of code throws an exception:

Exception in thread ' main ' java.util.ConcurrentModificationException
at java.util.arraylist$ Sublist.checkforcomodification (arraylist.java:1231)
at Java.util.arraylist$sublist.listiterator ( arraylist.java:1091)
at Java.util.AbstractList.listIterator (abstractlist.java:299)
at java.util.arraylist$ Sublist.iterator (arraylist.java:1087)
at java.util.AbstractCollection.toString (abstractcollection.java:454)
at Java.lang.String.valueOf (string.java:2994)
@ java.io.PrintStream.println (printstream.java:821)
at Test. Testsublist.main (testsublist.java:21)
at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:62)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)
at Java.lang.reflect.Method.invoke (method.java:498)
at Com.intellij.rt.execution.application.AppMain.main ( appmain.java:147)

Process finished with exit code 1




Sublist in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.