Updated June 6, 2023
Introduction to Scala ArrayBuffer
Scala ArrayBuffer is an indexed Sequence mutable data structure that allows us to add or change elements at a specific index. In ArrayBuffer we need not worry about the size; the size of an array buffer can be changed. It is mutable in nature. Random access to elements with the help of Array Buffer is high-speed. ArrayBuffer provides all the common methods for Sequence.
Syntax and Parameters
For defining an Array Buffer, we use the following syntax.
We need to import the scala mutable Arraybuffer like this:-
import scala.collection.mutable.ArrayBuffer
Val a = ArrayBuffer[datatype]()
// We define the datatype whatever we want to use over making a scala Array Buffer. It can be int, String, double, etc.
Scala ArrayBuffer Working with Examples
ArrayBuffer contains the array’s size and the elements in that Array. Before inserting any object in the array buffer, first, the size of the array is checked if the size is capable enough to put data elements inside it, the element is added, or else a new array with a bigger size is created, and the element is inserted inside it. All the elements are copied to the new Array.
Also, if we want to access the element, the index is traversed and checked for that element to see if it is present, and then only the element is traversed.
The Keyword new is not necessary to invoke an object for ArrayBuffer because it has an apply method with it. So we can create an object for an ArrayBuffer directly.
Let us check out an example:
1. Ways to create an ArrayBuffer:
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
2. Without the use of a new keyword.
val a = ArrayBuffer[Int]()
a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
3. With the use of a new keyword.
val b = new ArrayBuffer[Int]()
b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
4. String type array buffer.
val b = new ArrayBuffer[String]()
b: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer()
5. Double-type array buffer.
val b = new ArrayBuffer[Double]()
b: scala.collection.mutable.ArrayBuffer[Double] = ArrayBuffer()
We can also use the .toBuffer method to create an ArrayBuffer. Also, the ArrayBuffer.range function will generate the ArrayBuffer as given.
Let us check that with an example:
(1 to 5).toBuffer
res16: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
(1 until 5).toBuffer
res17: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3, 4)
('a' to 'c').toBuffer
res18: scala.collection.mutable.Buffer[Char] = ArrayBuffer(a, b, c)
("String1").toBuffer
res19: scala.collection.mutable.Buffer[Char] = ArrayBuffer(S, t, r, i, n, g, 1)
ArrayBuffer.range(1,5)
res22: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4)
Even fill and tabulate methods can also be used to create an ArrayBuffer.
Example #1 – ADDING elements in an ArrayBuffer
b+="This"
res4: b.type = ArrayBuffer(String1, This)
b+="is"
res5: b.type = ArrayBuffer(String1, This, is)
b+="a"
res6: b.type = ArrayBuffer(String1, This, is, a)
b+="sample string"
res7: b.type = ArrayBuffer(String1, This, is, a, sample string)
b+="Arraybuffer"
res8: b.type = ArrayBuffer(String1, This, is, a, sample string, Arraybuffer)
println(b)
ArrayBuffer(String1, This, is, a, sample string, Arraybuffer)
Code Snippet:
Example #2 – Adding Two or More Elements
We can use the append method to append two or more elements in an ArrayBuffer simultaneously. Let us check with an example:
val c = ArrayBuffer[String]()
c: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer()
c+=("Adding","more","than","one","elements")
res10: c.type = ArrayBuffer(Adding, more, than, one, elements)
c.append("Other Method!","for Adding")
println(c)
ArrayBuffer(Adding, more, than, one, elements, Other Method!, for Adding)
Code Snippet:
Example #3 – Accessing Elements in ArrayBuffer
We can access the elements from an array buffer by traversing the index of an ArrayBuffer.
Let us check that with an example:
println(c)
ArrayBuffer(Adding, more, than, one, elements, Other Method!, for Adding)
for(i<- 0 to c.length-1){
| println(c(i))
| }
Adding
more
than
one
elements
Other Method!
for Adding
c(0)
res33: String = Adding
c(1)
res34: String = more
c(2)
res35: String = than
c(3)
res36: String = one
Example #4 – Deleting Elements from Array Buffer
We can remove elements from an Array Buffer using the (-) operator. This removes the elements from an ArrayBuffer.
Even we can use remove and clear to remove an element or clear it from an ArrayBuffer.
val a = ArrayBuffer("Arpit","Anand","String1")
a: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(Arpit, Anand, String1)
a-= "Arpit"
res0: a.type = ArrayBuffer(Anand, String1)
a.remove(0)
res1: String = Anand
a.clear
println(a)
ArrayBuffer()
We can use the reducetoSize function to reduce the length of ArrayBuffer, whatever length we want to.
Let us check that with an Example:
val a = ArrayBuffer("Arpit","Anand","String1","String2")
a: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(Arpit, Anand, String1, String2)
a.reduceToSize(2)
println(a)
ArrayBuffer(Arpit, Anand)
This reduces the size of ArrayBuffer to the length of 2.
Example #5 – Updating Elements in Array Buffer
You can update an array of buffer elements within an ArrayBuffer by using the “update” method on the elements.
Let us check that with an Example:
val a = ArrayBuffer("Arpit","Anand","String1","String2")
a: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(Arpit, Anand, String1, String2)
a.update(0,"UpdatedString")
println(a)
ArrayBuffer(UpdatedString, Anand, String1, String2)
So this gives an updated ArrayBuffer over.
The Head and Tail Function will provide the Head and Tail of an array buffer.
ArrayBuffer(UpdatedString, Anand, String1, String2)
a.head
res8: String = UpdatedString
a.tail
res9: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(Anand, String1, String2)
The .take() function will take the elements inside it.
a.take(2)
res10: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(UpdatedString, Anand)
The .isEmpty() function checks whether the Arraybuffer is Empty or not.
a.isEmpty
res12: Boolean = false
The .toArray function is used to change the Arraybuffer into Array.
a.toArray
res13: Array[String] = Array(UpdatedString, Anand, String1, String2)
From the above method and function, we saw how ArrayBuffers works and its associated function.
Conclusion – Scala ArrayBuffer
From the above article, we came across the working and various concepts for ArrayBuffer in Scala. We came across the multiple functions and methods used by the ArrayBuffer class and learned the functionalities there. With the multiple examples, we saw how ArrayBuffer could be used with different data types and the idea behind using it.
So the above article concludes the proper usage, syntax, and functionalities of ArrayBuffer.
Recommended Articles
We hope that this EDUCBA information on “Scala ArrayBuffer” was beneficial to you. You can view EDUCBA’s recommended articles for more information.