How to add elements to a List in Scala
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 String.
Prepending elements to Scala Lists
One thing you can do when working with a Scala List is to create a newList from 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 namedp1 contains one string, then p2 contains two strings, and finally p3 contains 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 a Listand is also mutable (you can add and remove elements in it), use theListBuffer class instead, like this:
import scala.collection.mutable.ListBuffer var fruits = new ListBuffer[String]()
fruits += "Apple"
fruits += "Banana"
fruits += "Orange"
Then convert it to a List if/when you need to:
val fruitsList = fruits.toList
Scala REPL example
Here's what this List and ListBuffer example 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 Scala Listlike I'd work with a Java List (ArrayList, LinkedList), and this approach suits me.
How to add elements to a List in Scala的更多相关文章
- How do I add elements to a Scala List?
Scala List FAQ: How do I add elements to a Scala List? This is actually a trick question, because yo ...
- openmesh - src - trimesh delete and add elements
openmesh - src - trimesh delete and add elements openmesh 版本 8.1 About 本文主要介绍openmesh的如下接口 add_verte ...
- select 下拉菜单Option对象使用add(elements,index)方法动态添加
原生js 的add函数为下拉菜单增加选项 1.object.add(oElement [, iIndex]) index 可选参数:指定元素放置所在的索引号,整形值.如果没有指定值,将添加到集合的最后 ...
- jquery add() 和js add()
HTML DOM add() 方法 HTML DOM Select 对象 定义和用法 add() 方法用于向 <select> 添加一个 <option> 元素. 语法 sel ...
- Python高手之路【三】python基础之函数
基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...
- python-基本数据类型
/int整数/ 如: 18.73.84 每一个整数都具备如下功能: class int(object): """ int(x=0) -> int or long i ...
- Unity 最佳实践
转帖:http://www.glenstevens.ca/unity3d-best-practices/ 另外可以参考:http://devmag.org.za/2012/07/12/50-tips- ...
- Beginning Scala study note(6) Scala Collections
Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...
- Beginning Scala study note(2) Basics of Scala
1. Variables (1) Three ways to define variables: 1) val refers to define an immutable variable; scal ...
随机推荐
- php如何实现页面跳转
•PHP页面跳转一.header()函数 header()函数是PHP中进行页面跳转的一种十分简单的方法.header()函数的主要功能是将HTTP协议标头(header)输出到浏览器. header ...
- Linux内核结构体--kfifo 环状缓冲区
转载链接:http://blog.csdn.net/yusiguyuan/article/details/41985907 1.前言 最近项目中用到一个环形缓冲区(ring buffer),代码是由L ...
- 1z0-052 q209_10
10: A user, who is authenticated externally, logs in to a remote machine and connects to the databas ...
- vue 常见报错问题
情况一:http://eslint.org/docs/rules/no-tabs Unexpected tab character 解决方案:缩进是4个空格,而不是tab,设置indent 情况二 ...
- eclipse下java中凝视字体太小和xml中中文字体太小问题解决方法
我们在win7下进行android应用开发.须要搭建对应的开发环境.如今普遍基本上都是eclipse+adt+sdk,在本人搭建完环境后,发现eclipse下.java中的凝视和xml中的中文字体变得 ...
- 【FAI】七日年化收益与万份收益的理解
七日年化收益:其实指的一年的收益(应该忽略”七日”),这里的七日指的是取最近七日年化的结果 万份收益:每万元每天的收益 可以使用下图来清晰识别: 例子: 10000元按照5%的七日年化收益计算的话: ...
- 【Linux】在Linux上查看并替换特殊字符
现有windows上新建的一个txt文件file01.txt,内容如下: 我们通过ftp上传到Linux,在Linux下使用命令cat –A file01.txt查看文件内容发现该文件的结尾全是^M$ ...
- CentOS 6.8 ftp服务安装配置 基于本地用户和虚拟用户
CentOS 6.8 ftp服务安装配置 基于本地用户和虚拟用户 一.安装ftp服务 1.检查是否已经安装 # rpm -qa | grep ftp ftp-0.17-54.el6.x86_64 vs ...
- OpenWrt 中安装配置Transmission
参考文章https://wiki.openwrt.org/doc/uci/transmission 1. 安装包 必装的 transmission-daemon-openssl (后台服务)选装的 t ...
- 客户端用java api 远程操作HDFS以及远程提交MR任务(源码和异常处理)
两个类,一个HDFS文件操作类,一个是wordcount 词数统计类,都是从网上看来的.上代码: package mapreduce; import java.io.IOException; impo ...