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 ...
随机推荐
- OA项目实战学习(7)——初始化数据&权限配置显示
详细有哪些功能: 初始化数据 权限数据. 超级管理员. Installer.java package cn.xbmu.oa.install; import javax.annotation.Resou ...
- 腾讯地图api将物理地址转化成坐标
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- tomcat占用cpu过高解决办法
在工作中经常遇到tomcat占用cpu居高不下,针对这种情况有以下处理办法进行排查. jps --> 查看java的进程 top -Hp pid --> 根据jps得到的进程号(pid), ...
- 【LeetCode】128. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 阿里云ECS服务器Linux环境下配置php服务器(二)--phpMyAdmin篇
上一篇讲了PHP服务器的基本配置,我们安装了apache,php,还有MySQL,最后还跑通了一个非常简单的php页面,有兴趣的朋友可以看我的这篇博客: 阿里云ECS服务器Linux环境下配置php服 ...
- Android四款系统架构工具
开发者若想开发出一款高质量的应用,一款功能强大的开发工具想必是不可或缺的.开发工具简化了应用的开发流程,也能使开发者在应用开发本身投入更多的精力.本文就为大家带来4款实用的Android应用架构工具. ...
- RabbitMQ消息队列名词解释[转]
从AMQP协议可以看出,MessageQueue.Exchange和Binding构成了AMQP协议的核心,下面我们就围绕这三个主要组件 从应用使用的角度全面的介绍如何利用Rabbit MQ构建 ...
- RHEL7 -- 通过gerp使用正则表达式
正则表达式常会含有shell元字符(如S.*等),建议使用单引号('')来括起行令上的正则表达式 1.行定位符号 行首定位符号^和行尾定位符$ #找出以s开头的行: # grep '^s' /etc/ ...
- Error parsing XML: not well-formed (invalid token) 报错
鼠标右键选择Source然后再选Format 就可以解决此问题
- Spring Boot之HelloWorld
视频网址:http://www.iqiyi.com/w_19ruksbpf1.html <project xmlns="http://maven.apache.org/POM/4.0. ...