Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a program to demonstrate Array and ArrayBuffer objects in Scala.
- Scala arrays are similar to Java arrays.
- ArrayBuffer will provide a way to add elements at the beginning(+=:) and ending(+=) of ArrayBuffer object.
Let’s look at a simple program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.clearurdoubt import scala.collection.mutable.ArrayBuffer object ArrayBufferDemo { def main(args: Array[String]) { val array = new Array[String](3) val arrayBuffer = new scala.collection.mutable.ArrayBuffer[String] array(0) = "Hi, " array(1) = "Hello" array(2) = " World!" println(array.toList.mkString) arrayBuffer += "Hello" arrayBuffer += " World!" "Hi, " +=: arrayBuffer println(arrayBuffer.toList.mkString) } } |
Output:
Happy Learning.
Please leave a reply in case of any queries.