Scala - How to append varargs parameters to an ArrayBuffer? Code Example

The code for How to append varargs parameters to an ArrayBuffer?

import scala.collection.mutable.ArrayBuffer

val arrBuf = ArrayBuffer[String]()

// Adding one element
arrBuf += "New Delhi"

// Creating a method with a varargs parameter
def appendMultipleVarArgs(strings: ArrayBuffer[String], varArguments: String*): Unit = 
    arrBuf.appendAll(varArguments)

// Adding multiple varargs parameters
appendMultipleVarArgs(arrBuf, "Mumbai", "Bhopal")

// Printing elements
println(arrBuf)

/*
Output:
ArrayBuffer(New Delhi, Mumbai, Bhopal)
*/
Code by IncludeHelp, on August 13, 2022 22:07

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.