Removing Items from a ListBuffer

According to this publication , it is said that ListBuffer allows permanent deletion of the first and last elements. I studied the API link and ListBuffer source code, but cannot find how I am removing the last item in constant time, and remove(0) will do the job for the first item. What would be the correct way to remove the last item?

Another question: is it possible to effectively delete an item when iterating through a ListBuffer? In Java, this can be done using Iterator.remove() , but the Scala iterator does not seem to have a remove() method ...

+4
source share
2 answers

The first question has a simple but disappointing answer: you cannot delete the last element in constant time, as this will require a link to the element to the last. (This is a separate list, inside a wrapper class that contains the start and end elements of a list.)

The second question is equally simple and perhaps disappointing: The Scala Iterator is just a collection view. They do not modify the base collection. (This corresponds to the “immutable by default, mutable only when necessary” philosophy.)

+1
source

You can remove the last item with trimEnd(1)

+1
source

Source: https://habr.com/ru/post/1440129/


All Articles