PackageCom.test.io;ImportJava.io.BufferedReader;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException;Importjava.util.LinkedList;ImportCom.mysql.jdbc.Buffer; Public classReadkeyfile { Public Static voidMain (string[] args)throwsIOException {BufferedReader BufferedReader=NULL; Try{BufferedReader=NewBufferedReader (NewFileReader ("C:/users/administrator/desktop/test.txt")); LinkedList<String> list =NewLinkedlist<string>(); String Str=Bufferedreader.readline (); while((Str=bufferedreader.readline ())! =NULL) {System.out.println (str); List.add (str); } System.out.println ("------------"); for (String string:list) {System.out.println (List.removelast ()); } } Catch(Exception e) {E.printstacktrace (); } finally { if(BufferedReader! =NULL) Bufferedreader.close (); } }}
Results and Error:
222222222222222222223333333333333333333344444444444444444444555555555555555555556666666666666666666677777777777777777777 ------------77777777777777777777java.util.ConcurrentModificationException at java.util.linkedlist$ Listitr.checkforcomodification (Linkedlist.java:966) at Java.util.linkedlist$listitr.next ( Linkedlist.java:888) at com.test.io.ReadKeyFile.main (Readkeyfile.java:24)
Test.txt:
1111111111111111111122222222222222222222333333333333333333334444444444444444444455555555555555555555666666666666666666667 7777777777777777777
If you replace the red part of the source code with the following, you can output normally, and the problem is not visible in the Remvelast () method, but in the iteration.
System.out.println (List.removelast ()); System.out.println (List.removelast ());
Find a text on the Internet:
1. The iteration of the collection using the enhanced for loop is actually an iterator by default;
2. Therefore, you cannot use the collection's reference variable to manipulate the collection directly in a loop to avoid security exceptions that result in multithreaded concurrent access.
1 means that the enhanced for loop is traversed in a similar way as:
iterator<string> Iterator = list.iterator (); while (Iterator.hasnext ()) System.out.println (iterator.next ());
In view of this, it is necessary to look at the LinkedList code. In LinkedList, this iterator is listitr, it is inherited from the Itr class, the next () method is derived from the ITR class, in the inside Find Next () The source code is as follows:
PublicE Next () { checkforcomodification (); Try { inti =cursor; E Next=get (i); Lastret=i; Cursor= i + 1; returnNext; } Catch(indexoutofboundsexception e) {checkforcomodification (); Throw Newnosuchelementexception (); }}
Note Checkforcomodification (), this is the place to report the error, specifically in the following place the error:
Final void checkforcomodification () { if (modcount! = expectedmodcount )throw New concurrentmodificationexception ();}
According to our test: LinkedList print out the first row of 7 is an error.
Modcount This should be 7 after the first row of the value should be 8, because add 7 times, and Removelast again
How much is that expect?
Look at the ITR source code, really messy, and really do not want to read the comments, I still go over the Java programming ideas first
Linkedlist-java.util.concurrentmodificationexception