The following code throws an UnsupportedOperationException
with Scala 2.12.7 and Java 11.0.1.
listBuffer
.iterator
.dropWhile(_ != u)
.asJava
.remove()
Why? ListBuffer
is mutable, and using an Iterator
to remove elements while traversing should be valid. The ListBuffer
contains Int
.
java.lang.UnsupportedOperationException was thrown.
java.lang.UnsupportedOperationException
at scala.collection.convert.Wrappers$IteratorWrapper.remove(Wrappers.scala:31)
at scala.collection.convert.Wrappers$IteratorWrapper.remove(Wrappers.scala:26)
at week4.UndirectedGraph.$anonfun$removeVertex$2(UndirectedGraph.scala:52)
at scala.runtime.java8.JFunction1$mcVI$sp.apply(JFunction1$mcVI$sp.java:12)
at scala.collection.immutable.List.foreach(List.scala:388)
at scala.collection.generic.TraversableForwarder.foreach(TraversableForwarder.scala:34)
at scala.collection.generic.TraversableForwarder.foreach$(TraversableForwarder.scala:34)
at scala.collection.mutable.ListBuffer.foreach(ListBuffer.scala:43)
at week4.UndirectedGraph.$anonfun$removeVertex$1(UndirectedGraph.scala:50)
Edit:
Not a duplicate of this question, since the problem with that is trying to structurally modify the list returned by Arrays.asList
. I shouldn't have to state the obvious, if not for the people who don't know the answer, but are usually the first to try to close a question as duplicate.