-4

I am programming a sorting system to school that must split some people into two cars, but I am stuck and I don't know how to continue.

When I delete the second when, it was working. But I need it there so I don't know what to do.

I already tried iterators but I am new in kotlin and it didn't work.

for (firstPeople in firstCar){
                    when {
                        k.contains(firstPeople.toString()) -> secondCar.add(j)
                        // println(secondCar)
                        k.contains(firstPeople.toString()) -> firstCar.add(j)
                        // println(firstCar)
                        else -> when {
                            firstCar.size > secondCar.size -> secondCar.add(j)
                            firstCar.size < secondCar.size -> firstCar.add(j)
                            else -> firstCar.add(j)
                        }
                    }
                }

Error:

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at MainKt.main(Main.kt:65)
at MainKt.main(Main.kt)

Thank you so much for the answer.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Stepan
  • 5
  • 3
  • 1
    When iterating in this way, you can't modify the list. This one doesn't make much sense anyhow, you're going through each element of `firstCar`, and then adding to `firstCar`? – Rogue Oct 15 '19 at 13:51
  • You cannot iterate over `firstCar` and at the same time add elements `firstCar.add(j)` – QBrute Oct 15 '19 at 13:52
  • if that second `k.contains(firstPeople.toString())` wouldn't be redundant, it would have failed already there... your list does not support concurrent modifications, therefore you should not add or remove items while iterating over it... (which you do with `firstCar.add(j)`)... from what I see I don't really know how your sort should work, but I am pretty sure this can be solved easier... by the way... you said you tried iterator, but it didn't work... maybe you should also add what you tried so far and tell what didn't work? – Roland Oct 15 '19 at 13:56

1 Answers1

2

Looks like you are using and ArrayList. And you are inserting an item in it while iterating via iterator: (firstCar.add(j). Here is what its JavaDoc says:

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. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

It means that you may modify the collection only using the iterator methods, and not directly.

Try to avoid list modification, or use iterator's add method, or use copy on write collections.

madhead
  • 31,729
  • 16
  • 153
  • 201