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 List
and 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 List
like 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 ...
随机推荐
- Modifying a Request or Response
To make custom changes to web requests and responses, use FiddlerScript to add rules to Fiddler's On ...
- svn 和 git的区别
1.速度: 克隆一份全新的目录,以同样拥有五个(才五个)分支来说,SVN是同时复製5个版本的文件,也就是说重复五次同样的动作.而Git只是获取文件的每个版本的元素,然后只载入主要的分支(master) ...
- jquery动态修改div高度
<!DOCTYPE html> <html> <head> <script src="jquery-1.4.2.min.js">&l ...
- java String字符串
五.java数据类型之String(字符串) CreateTime--2017年7月21日16:17:45 Author:Marydon (一)数据格式 (二)初始化 // 方式一 String ...
- 【Docker】安装tomcat并部署应用
安装tomcat 1.拉取tomcat镜像 docker pull docker.io/tomcat 查看镜像 docker images 2.启动tomcat 首先添加8090端口:firewall ...
- 如何使php页面中不再出现NOTICE和DEPRECATED的错误提示
在php.ini配置文件中修改: error_reporting=E_ALL & ~E_NOTICE & ~E_DEPRECATED 亲测有效,拿去用吧
- 基于docker的centos:latest镜像制作nginx的镜像
Dockerfile和nginx.repo在同一目录下 先创建nginx.repo [root@localhost nginx]# cat nginx.repo [nginx] name=nginx ...
- MySQL JOIN操作报错问题小解
1 问题描述 在调用一个MySQL存储过程的时候,有时候会出现下面的错误: Illigal mix of collations(gbk\_chinese\_ci, IMPLICIT) and (lat ...
- HDUOJ-----取(m堆)石子游戏
取(m堆)石子游戏 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- linux创建新用户及权限
在Linux中添加普通新用户 ,超级用户(也称为“root”)是一个具有修改系统中任何文件权力的特别账号.在日常工作中,最好不要使用超级用户账号进入系统,因为任何错误操作都可能导致巨大的损失.由于超级 ...