How do I add elements to a Scala List?
2016-07-05 22:20
483 查看
Scala List FAQ: How do I add elements to a Scala List?
This is actually a trick question, because you can't add elements to a ScalaList; it's an immutable data structure, like a Java
Those examples show how to create a series of lists. The initial list named
While that approach looks cumbersome in a small example, it makes sense in larger, real-world code. You can see more/better examples of this approach in my tutorial titled, Scala List class examples.
This is actually a trick question, because you can't add elements to a ScalaList; it's an immutable data structure, like a Java
String.
Prepending elements to Scala Lists
One thing you can do when working with a ScalaListis to create a new
Listfrom an existing
List. This sort of thing is done often in functional programming, and the general approach looks like this:
scala> val p1 = List("Kim") p1: List[String] = List(Kim) scala> val p2 = "Julia" :: p1 p2: List[String] = List(Julia, Kim) scala> val p3 = "Judi" :: p2 p3: List[String] = List(Judi, Julia, Kim)
Those examples show how to create a series of lists. The initial list named
p1contains one string, then
p2contains two strings, and finally
p3contains three strings.
While that approach looks cumbersome in a small example, it makes sense in larger, real-world code. You can see more/better examples of this approach in my tutorial titled, Scala List class examples.
Use a ListBuffer when you want a "List" you can modify
If you want to use a Scala sequence that has many characteristics of aListand is also mutable (you can add and remove elements in it), use theListBuffer class instead, like this:
() fruits += "Apple" fruits += "Banana" fruits += "Orange" Then convert it to a ]val fruitsList = fruits.toList
Scala REPL example
Here's what thisListand
ListBufferexample looks like using the Scala command line (REPL):
scala> import scala.collection.mutable.ListBuffer import scala.collection.mutable.ListBuffer scala> var fruits = new ListBuffer[String]() fruits: scala.collection.mutable.ListBuffer[String] = ListBuffer() scala> fruits += "Apple" res0: scala.collection.mutable.ListBuffer[String] = ListBuffer(Apple) scala> fruits += "Banana" res1: scala.collection.mutable.ListBuffer[String] = ListBuffer(Apple, Banana) scala> fruits += "Orange" res2: scala.collection.mutable.ListBuffer[String] = ListBuffer(Apple, Banana, Orange) scala> val fruitsList = fruits.toList fruitsList: List[String] = List(Apple, Banana, Orange)
More functional ways to work with Scala lists
Depending on your needs, there are other, "more functional" ways to work with Scala lists, and I work through some of those in my Scala List examples. But for my needs today, I just wanted to work with a ScalaListlike I'd work with a Java List (
ArrayList,
LinkedList), and this approach suits me.
相关文章推荐
- geeksforgeeks@ Find sum of different corresponding bits for all pairs (Bit manipulation)
- spring学习(三)—AOP
- Tomcat漏洞
- 《leetCode》: Maximal Square
- android中的资源文件
- hdu:1556 Just a Hook
- 在android studio中新建android gradle project的时候connect refused:connect或者卡在building project...或Refreshing
- leetcode:Rotate List
- Java基础知识(一)
- centos7安装zabbix3.0.3
- tomcat 配置虚拟站点
- Cache应用中的服务过载案例研究
- Ubuntu入门学习笔记(1)
- Can you share some Scala List class examples?
- 类的继承和接口的实现
- pager-taglib分页中文乱码
- Python安装Pip出现的问题
- JSON详解
- 199_让PieChart出来之后动画
- 反人类的MyEclipse之-eclipse设置花括号换行显示