java - ConcurrentModificationException is thrown when it should not -


any ideas on why

lines = newremaininglines.size(); 

would throw exception?

lines 

is int using store size of arraylist

newremaininglines(); 

i realize not enough give definitive answer, perhaps explanation on helpful. don't see how code concurrently modifying int.

int = 0;     polines.clear();     itemlines.clear();     lines = newlines.size();     //polines should contain lines not relating items     while(!newlines.get(i).equals(itemstart) && < lines){         polines.add(newlines.get(i));         i++;     }     list<string> newremaininglines = newlines.sublist(i + 1, lines);     newlines.clear();     = 0;     lines = newremaininglines.size();     //itemlines should contain lines relating items     while(!newremaininglines.get(i).equals(poend) && < lines){          itemlines.add(newremaininglines.get(i));         i++;     }     list<string> finalremaininglines = newremaininglines.sublist(i + 1, lines);     = 0;     lines = finalremaininglines.size();     while(i < lines){          newlines.add(finalremaininglines.get(i));         i++;     }     temporder = pof.createpurchaseorder(polines,itemlines);     _orders.add(temporder); 

this appears in if-else statement inside for-loop similar segments of code. final one, 1 throws exception.

sublist returns view of underlying list. not copy.

quoting javadoc:

the returned list backed list, non-structural changes in returned list reflected in list, , vice-versa.

the semantics of list returned method become undefined if backing list (i.e., list) structurally modified in way other via returned list.

the statement newlines.clear() structurally modifies backing list, subsequent call newremaininglines.size() says no-can-do!, aka concurrentmodificationexception.

to create copy, use:

list<string> finalremaininglines = new arraylist<>(newremaininglines.sublist(i + 1, lines)); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -