How to declare an empty list and then add a line to scala?

I have a code like this:

val dm = List[String]() val dk = List[Map[String,Object]]() ..... dm.add("text") dk.add(Map("1" -> "ok")) 

but it fires runtime java.lang.UnsupportedOperationException.

I need to declare an empty list or empty cards, and some where later in the code I need to fill them.

+55
scala
Jul 02 2018-11-11T00:
source share
6 answers

Scala lists are immutable by default. You cannot “add” an item, but you can create a new list by adding a new item in front. Since this is a new list, you need to reassign the link (so you cannot use val).

 var dm = List[String]() var dk = List[Map[String,AnyRef]]() ..... dm = "text" :: dm dk = Map(1 -> "ok") :: dk 

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

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

NB: In scala, do not use the Object type, but Any , AnyRef or AnyVal .

+82
Jul 02 2018-11-11T00:
source share

If you need to mutate things, use ArrayBuffer or LinkedBuffer . However, it would be better to refer to this statement:

I need to declare an empty list or empty cards and some where later in the code you need to fill them.

Instead, populate the list with code that returns items. There are many ways to do this, and I will give some examples:

 // Fill a list with the results of calls to a method val l = List.fill(50)(scala.util.Random.nextInt) // Fill a list with the results of calls to a method until you get something different val l = Stream.continually(scala.util.Random.nextInt).takeWhile(x => x > 0).toList // Fill a list based on its index val l = List.tabulate(5)(x => x * 2) // Fill a list of 10 elements based on computations made on the previous element val l = List.iterate(1, 10)(x => x * 2) // Fill a list based on computations made on previous element, until you get something val l = Stream.iterate(0)(x => x * 2 + 1).takeWhile(x => x < 1000).toList // Fill list based on input from a file val l = (for (line <- scala.io.Source.fromFile("filename.txt").getLines) yield line.length).toList 
+16
Jul 02 2018-11-11T00:
source share

As already mentioned, this is not the best way to use lists in Scala ...

 scala> val list = scala.collection.mutable.MutableList[String]() list: scala.collection.mutable.MutableList[String] = MutableList() scala> list += "hello" res0: list.type = MutableList(hello) scala> list += "world" res1: list.type = MutableList(hello, world) scala> list mkString " " res2: String = hello world 
+11
Jul 02 2018-11-11T00:
source share

By default, collections in scala are immutable, so you have a + method that returns a new list with an element added to it. If you really need something like the add method, you need a volatile collection, for example. http://www.scala-lang.org/api/current/scala/collection/mutable/MutableList.html , which has a + = method.

+2
Jul 02 2018-11-11T00:
source share

Perhaps you can use ListBuffers in scala to create an empty list and add rows later, because ListBuffers are mutable. Also, all List functions are available for ListBuffers in scala.

 import scala.collection.mutable.ListBuffer val dm = ListBuffer[String]() dm: scala.collection.mutable.ListBuffer[String] = ListBuffer() dm += "text1" dm += "text2" dm = ListBuffer(text1, text2) 

if you want, you can convert this to a list using .toList

0
Jun 16 '17 at 6:23
source share

In your case, I use: val dm = ListBuffer[String]() and val dk = ListBuffer[Map[String,anyRef]]()

0
Aug 03 '17 at 10:03 on
source share



All Articles