ArrayBuffer
In this page:
ArrayBuffer
ArrayBuffer (from scala.collection.mutable) is a resizable, mutable sequence, similar to a dynamic array or ArrayList in other languages. Unlike Array, it can grow or shrink with += and -= in addition to allowing element mutation. It's the go-to mutable collection when you need to build up a sequence incrementally.
Note: Use ArrayBuffer instead of Array when the collection's size needs to change after creation, not just its element values.
Example: ArrayBuffer
import scala.collection.mutable.ArrayBuffer
object Main extends App {
val buffer = ArrayBuffer(1, 2, 3)
buffer += 4
buffer += 5
println(buffer)
println(buffer.length)
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: