Take a look at the javadoc for ArrayList:
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.
An enhanced for-loop that you've used like on this line:
for(char[] char1 : record){
implicitly creates a list iterator and iterates over it.
The problem arises on the line:
record.add(char2);
where you are modifying the list as you iterate through it, you'll need to use the list's iterator to iterate through and add appropriately.
A possible solution would be to manually retrieve the list iterator and iterate, this will expose access to the add()
method which can be used to safety add items to the list as you iterate through. For example:
// Here we manually retrieve the list iterator to work with
ListIterator<char[]> iter = record.listIterator();
// Iterating through the entire list is done via the `hasNext()` method
while(iter.hasNext()){
char[] char1 = iter.next();
if(Arrays.equals(char1, char2)){
return false;
}
// Note: the add() method can only be called once per next() method call
iter.add(char2);
}
Note that once added, the list iterator will eventually iterate into the newly added elements. To prevent this, you can add the new elements to a temporary holding list and then transfer across as appropriate (such as when iteration is complete).