博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to add elements to a List in Scala
阅读量:6441 次
发布时间:2019-06-23

本文共 2418 字,大约阅读时间需要 8 分钟。

Scala List FAQ: How do I add elements to a  List?

This is actually a trick question, because you can't add elements to a Scala; 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" :: p1p2: List[String] = List(Julia, Kim)scala> val p3 = "Judi" :: p2p3: 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, .

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 the class instead, like this:

import scala.collection.mutable.ListBuffervar 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.ListBufferimport scala.collection.mutable.ListBufferscala> 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.toListfruitsList: 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 . 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.

转载地址:http://drdwo.baihongyu.com/

你可能感兴趣的文章
css3 Gradients 线性渐变
查看>>
ucfirst() 函数
查看>>
bootstrap-导航条层次的导航
查看>>
git rm使用
查看>>
xss***代码
查看>>
Python学习网站
查看>>
mybatis基础(一)
查看>>
Python的Django框架中的Context使用
查看>>
我的友情链接
查看>>
linux常用命令
查看>>
Docker在Windows系统下的安装及简单使用介绍
查看>>
CentOS用yum安装X Window
查看>>
gpfs 修改 副本数
查看>>
Ubuntu16.4安装Wordpress
查看>>
模块和包
查看>>
socket编程总结
查看>>
逻辑回归模型(LR)
查看>>
[读书笔记]JavaScript 闭包(Closures)
查看>>
Django restfulframework 开发相关知识 整理
查看>>
linux信息查看手记
查看>>