scala listbuffer
时间: 2023-10-07 10:06:17 浏览: 113
scala-scala
Scala ListBuffer is a mutable sequence data structure that allows you to add, remove, and modify elements in the list. It is similar to an array or a list, but with the added advantage of being mutable.
Here is an example of how to use ListBuffer in Scala:
```
import scala.collection.mutable.ListBuffer
// create a ListBuffer
val listBuffer = ListBuffer(1, 2, 3)
// add elements to the ListBuffer
listBuffer += 4
listBuffer ++= List(5, 6)
// remove elements from the ListBuffer
listBuffer -= 4
listBuffer.remove(2)
// modify elements in the ListBuffer
listBuffer(1) = 7
// print the ListBuffer
println(listBuffer)
```
Output:
```
ListBuffer(1, 7, 5, 6)
```
As you can see, ListBuffer allows you to add, remove, and modify elements in the list, making it a powerful tool for working with mutable sequences in Scala.
阅读全文