Let’s now populate newArrayBuffer. To add an element to an ArrayBuffer you use the assignment operator += which appends the specified element to the end of an ArrayBuffer.
xxxxxxxxxx
import scala.collection.mutable.ArrayBuffer
val newArrayBuff = new ArrayBuffer[Int]()
newArrayBuff += 6
newArrayBuff += 15
newArrayBuff += 78
newArrayBuff += 4
newArrayBuff += 32
newArrayBuff += 11
// Driver Code
newArrayBuff.foreach(println)
To add a single item, we can use the add() method. To add multiple items, we’d have to use update().
The input for update() must be another set, list, tuple, or string.
Let’s add elements to an empty set:
xxxxxxxxxx
empty_set = set()
print(empty_set)
empty_set.add(1)
print(empty_set)
empty_set.update([2, 3, 4, 5, 6])
print(empty_set)