I'm trying to add an item to a list of lists. partitions is a LinkedList of lists of Strings. I'm trying to add an item to the begining of one of the partitions in the list of partitions, but I'm getting a ConcurrentModificationException, even though I'm using a copy of the list called partitionsCopy.
Is there any way to do this? All I can find are examples on how to remove items or add items using ListIterator, but I can't add an item at a specific position with ListIterator
int index = 0;
for (List<String<?>> partition : partitions) {
if (index > 0) {
partitionsCopy.get( index ).add(0, lastPartition.get(lastPartition.size() - 1));
}
lastPartition = partition;
index++;
}
partitionsCopy looks like this
List<List<String<?>>> partitionsCopy = new LinkedList<List<String<?>>>( );
partitionsCopy.addAll( partitions );
Here's what I came up with from jtahlborn's answer.
for ( List<String<?>> partition : partitions ) {
List<String<?>> list = new ArrayList<String<?>>( );
list.addAll( partition );
partitionsCopy.add( list );
}
partitionsCopyreally a copy of theList, rather than just another reference to the sameList? Where do you initialize that variable?lastPartitiondeclared? Looks to me like it's probably a reference to the same list aspartitions, which would cause the issue.