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


i have code this:

val dm  = list[string]() val dk = list[map[string,object]]()  .....  dm.add("text") dk.add(map("1" -> "ok")) 

but throws runtime java.lang.unsupportedoperationexception.

i need declare empty list or empty maps , later in code need fill them.

scala lists immutable default. cannot "add" element, can form new list appending new element in front. since new list, need reassign reference (so can't use val).

var dm  = list[string]() var dk = list[map[string,anyref]]()  .....  dm = "text" :: dm dk = map(1 -> "ok") :: dk 

the operator :: creates new list. can use shorter syntax:

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

nb: in scala don't use type object any, anyref or anyval.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -