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 (ArrayListLinkedList), and this approach suits me.

How to add elements to a List in Scala的更多相关文章

  1. 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 ...

  2. openmesh - src - trimesh delete and add elements

    openmesh - src - trimesh delete and add elements openmesh 版本 8.1 About 本文主要介绍openmesh的如下接口 add_verte ...

  3. select 下拉菜单Option对象使用add(elements,index)方法动态添加

    原生js 的add函数为下拉菜单增加选项 1.object.add(oElement [, iIndex]) index 可选参数:指定元素放置所在的索引号,整形值.如果没有指定值,将添加到集合的最后 ...

  4. jquery add() 和js add()

    HTML DOM add() 方法 HTML DOM Select 对象 定义和用法 add() 方法用于向 <select> 添加一个 <option> 元素. 语法 sel ...

  5. Python高手之路【三】python基础之函数

    基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...

  6. python-基本数据类型

    /int整数/ 如: 18.73.84 每一个整数都具备如下功能: class int(object): """ int(x=0) -> int or long i ...

  7. Unity 最佳实践

    转帖:http://www.glenstevens.ca/unity3d-best-practices/ 另外可以参考:http://devmag.org.za/2012/07/12/50-tips- ...

  8. Beginning Scala study note(6) Scala Collections

    Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...

  9. 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 ...

随机推荐

  1. 算法笔记_175:历届试题 蚂蚁感冒(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 长100厘米的细长直杆子上有n只蚂蚁.它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒. 当两只蚂蚁碰面时,它 ...

  2. 解决win10 获取不到ip

    1.检查Dhcp服务是否启动 2.关闭防火墙

  3. python之模块csv之CSV文件的写入(按行写入)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #CSV文件的写入(按行写入) import csv #csv文件,是一种常用的文本格式,用以存储表格数据,很 ...

  4. Linux下MySQL链接被防火墙阻止

    Linux下安装了MySQL,不能从其它机器访问 帐号已经授权从任意主机进行访问 vi /etc/sysconfig/iptables 在后面添加 -A RH-Firewall-1-INPUT -m ...

  5. linux too many open files报错

    修改方法:vi /etc/security/limits.conf  增加一行,如下: *       -       nofile          65535 修改vi /etc/ssh/sshd ...

  6. 微软 Visual Studio 2017 中文正式版下载 – 免费社区版/专业版/企业版

    作为“宇宙最强”的集成开发环境 IDE,微软的 Visual Studio 不仅破天荒发布了 macOS 版本,如今终于也推出了其 Windows 的最新版本—— VS 2017 正式版了.这对开发者 ...

  7. Xiuno 开发手册正式发布。

    下载地址:http://bbs.xiuno.com/down/xiuno.chm.tar.gz

  8. Java中网络相关API的应用——InetAddress&URL

    一.InetAddress类 标识网络上的硬件资源 package com.homework; import java.net.InetAddress; import java.net.Unknown ...

  9. nexus 介绍

    http://juvenshun.iteye.com/blog/349534

  10. 解决错误This message can only be sent over HTTPS.

    在做基于Owin oauth客户端申请授权,调用如下代码: this.InitializeWebServerClient(); var userAuthorization = _webServerCl ...