InterviewSolution
Saved Bookmarks
| 1. |
Explain how you will append data to a list. |
|
Answer» A Scala list is a collection of data stored as a linked list. In Scala, a list is IMMUTABLE, meaning it cannot be changed. To remedy this, Scala OFFERS List-Buffer. There are multiple ways to update a list and add new elements such as using the ":+" method: It adds new elements at the end of a list. Syntax: list_Name :+ element_NameExample: object Colours { DEF main(args: Array[String]) { val myList = List ("Red" , "White") println("List’s Content : " + myList) println(" Appending/adding elements at the end of list!") val updatedList = myList :+ "BLUE" println("UPDATED List’s Content: " + updatedList) } }Output: List’s Content : List(Red, White) Appending/adding elements at the end of the list! Updated List’s Content : List (Red, White, Blue) |
|