How to declare empty list and then add string in scala?

scala

Solution

Scala lists are immutable by default. You cannot "add" an element, but you can form a new list by appending the new element in front. Since it is a new list, you need to reassign the reference (so you can't use a val).

var dm  = List[String]()
var dk = List[Map[String,AnyRef]]()

.....

dm = "text" :: dm
dk = Map(1 -> "ok") :: dk

The operator `::` creates the new list. You can also use the shorter syntax:

dm ::= "text" 
dk ::= Map(1 -> "ok")

NB: In scala don't use the type `Object` but `Any`, `AnyRef` or `AnyVal`.

Problem

I have code like this: ``` val dm = List[String]() val dk = List[Map[String,Object]]() ..... dm.add("text") dk.add(Map("1" -> "ok")) ``` but it throws runtime java.lang.UnsupportedOperationException. I need to declare empty list or empty maps and some where later in the code need to fill them.

Original source