ArrayBuffer is a variable-length array. It is similar to ArrayList in Java.
Syntax
var/val variableName = new ArrayBuffer[DataType]
Example
val arrayBuffer1 = new ArrayBuffer[Int]
To use ArrayBuffer, you should import the below package.
scala.collection.mutable.ArrayBuffer
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
scala> val arrayBuffer1 = new ArrayBuffer[Int]
val arrayBuffer1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
How to add an element to ArrayBuffer?
Use + operator to add an element to the array.
scala> arrayBuffer1 += 2
val res136: arrayBuffer1.type = ArrayBuffer(2)
scala> arrayBuffer1 += 3
val res137: arrayBuffer1.type = ArrayBuffer(2, 3)
scala> arrayBuffer1 += 5
val res138: arrayBuffer1.type = ArrayBuffer(2, 3, 5)
scala> print (arrayBuffer1)
ArrayBuffer(2, 3, 5)
How to add elements from an Array to ArrayBuffer?
Use ++ operator.
scala> arrayBuffer1 ++= Array(7, 11, 13)
val res143: arrayBuffer1.type = ArrayBuffer(2, 3, 5, 7, 11, 13, 7, 11, 13)
How to remove an element from ArrayBuffer?
Use remove(index) method to remove the element at a specific index from ArrayBuffer.
scala> print(arrayBuffer1)
ArrayBuffer(2, 3, 5, 7, 11, 13, 7, 11, 13)
scala>
scala> arrayBuffer1.remove(3)
val res145: Int = 7
scala> arrayBuffer1.remove(7)
val res146: Int = 13
scala> print(arrayBuffer1)
ArrayBuffer(2, 3, 5, 11, 13, 7, 11)
How to insert an element at a specific index?
Use insert(index, element) method to insert an element at a specific index.
scala> print(arrayBuffer1)
ArrayBuffer(2, 3, 5, 11, 13, 7, 11)
scala> arrayBuffer1.insert(2, 123456)
scala> print(arrayBuffer1)
ArrayBuffer(2, 3, 123456, 5, 11, 13, 7, 11)
How get an array from ArrayBuffer?
Use toArray method.
scala> var arr1 = arrayBuffer1.toArray
var arr1: Array[Int] = Array(2, 3, 123456, 5, 11, 13, 7, 11)
How to get ArrayBuffer from an Array?
Use toBuffer method.
scala> var arrayBuffer2 = arr1.toBuffer
var arrayBuffer2: scala.collection.mutable.Buffer[Int] = ArrayBuffer(2, 3, 123456, 5, 11, 13, 7, 11)
Find sum of elements of ArrayBuffer?
‘sum’ function is used to get the sum of all the elements of ArrayBuffer.
scala> ArrayBuffer(2, 3, 5, 7).sum
val res158: Int = 17
Find the maximum element in an ArrayBuffer
Use ‘max’ function.
scala> ArrayBuffer(2, 30, 51, 7).max
val res160: Int = 51
Sort the elements of ArrayBuffer
Use ‘sorted’ function. This method does not sort in place, it returns completely new ArrayBuffer with sorted data.
scala> val buffer1 = ArrayBuffer(2, 30, 51, 7)
val buffer1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 30, 51, 7)
scala> val buffer2 = buffer1.sorted
val buffer2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 7, 30, 51)
scala> println(buffer1)
ArrayBuffer(2, 30, 51, 7)
scala> println(buffer2)
ArrayBuffer(2, 7, 30, 51)
Reverse the elements of ArrayBuffer
Use ‘reverse’ function to reverse the elements of ArrayBuffer.
scala> var buffer1 = ArrayBuffer(2, 30, 51, 7)
var buffer1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 30, 51, 7)
scala> var buffer2 = buffer1.reverse
var buffer2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(7, 51, 30, 2)
scala> buffer1
val res172: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 30, 51, 7)
scala> buffer2
val res173: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(7, 51, 30, 2)
Print elements of ArrayBuffer using a separator
Use ‘mkString’ method to get a string that has elements of ArrayBuffer where each element is separated by given separator.
scala> var res = buffer1.mkString(",")
var res: String = 2,30,51,7
scala> println(res)
2,30,51,7
Separate elements using |.
scala> res = buffer1.mkString("|")
// mutated res
scala> println(res)
2|30|51|7
No comments:
Post a Comment