Let's say I have a JSONArray
called table
in json-simple-1.1.1. I want to truncate it to a maximum number of values. To do so, I take a sub-set and reassign it like so:
List<Object> rows = this.table.subList(0, newSize);
table.clear();
table.addAll(rows);
However, doing this results in a java.util.ConcurrentModificationException
on the table.addAll(rows)
line. The kicker is, that there is only one thread. From reading about the exception on Oracle, it's evident that I'm doing some kind of invalid operation here, but I'm not sure what.
Here's code that will produce the error:
package recordIndexer.server.importer;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONValue;
public class Main {
public static void main(String[] args) {
JSONArray j = (JSONArray)JSONValue.parse("[\"Hello\", \"world\"]");
List<Object> sub = j.subList(0, 1);
j.clear();
j.addAll(sub);
}
}