How do I create a List in Scala?
Scala List class FAQ: How do I create a List in Scala?
You can create a Scala List in several different ways, including these approaches:
- Lisp style
- Java style
- Using the List class
rangemethod - Using the List class
fillmethod - Using the List class
tabulatemethod
In this Scala List tutorial, I'll demonstrate each of these approaches. I'll execute each command in the Scala command-line interpreter so you can see the results of each approach.
1) Create a Scala List in the Lisp style
First, if you prefer the Lisp-style of programming, you can create a ScalaList using the "cons" syntax, like this:
scala> val list = 1 :: 2 :: 3 :: Nil
list: List[Int] = List(1, 2, 3)
As you can see, this creates a List that contains the Ints 1, 2, and 3. With this approach, you need to end the list with the Nil object.
In this "cons" style, the :: method takes two arguments, a "head", which is a single element, and a "tail", which is a List. (And yes, :: is a function/method.)
2) Create a Scala List in the Java style
My guess is that the most popular way to create a List is with what I call the "Java style":
scala> val list = List(1,2,3)
x: List[Int] = List(1, 2, 3)
This syntax looks a lot like the Java way to create an object, except (a) you don't need the "new" keyword before the List, and (b) you don't have to declare the type of elements in the List.
Note that if you're going to mix types in a List constructor, you may need to manually specify the type of the List. This example demonstrates the syntax to specify the List type:
scala> val x = List[Number](1, 2.0, 33d, 0x1)
x: List[java.lang.Number] = List(1, 2.0, 33.0, 1)
In this example I've explicitly stated that the values in the List are of theNumber type.
3) Create a Scala List with the range method
Another convenient way to create a List is with the range method:
scala> val x = List.range(1, 10)
x: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
As you can see, this example creates a List of Int values, beginning at 1, and ending at 9.
In addition to this simple approach, the range function can also take a third argument, which serves as a "step" value when creating the List:
scala> val x = List.range(0, 10, 2)
x: List[Int] = List(0, 2, 4, 6, 8)
4) Create a Scala List with the List class fill method
Another convenient way to create a Scala List is with the fill method:
scala> val x = List.fill(3)("foo")
x: List[java.lang.String] = List(foo, foo, foo)
As you can see, you just specify how many items you want, and the object value you want to fill each List element with.
5) Create a Scala List with the List class tabulate method
Finally, you can create a Scala List with the tabulate method of the Listclass.
The tabulate method creates a new List whose elements are created according to the function you supply. The book Programming in Scalashows how to create a List using a simple "squares" function with thetabulate method:
scala> val x = List.tabulate(5)(n => n * n)
x: List[Int] = List(0, 1, 4, 9, 16)
As you can see, that example creates a List of five elements, where the element values are the square of the index of each element, so 0 becomes 0, 1 becomes 1, 2 becomes 4, 3 becomes 9, and 4 becomes 16.
6) Creating Scala Lists - Summary
In summary, as you have seen, you can create Scala lists in several different ways, including these approaches:
- Lisp style
- Java style
- Using the List class range method
- Using the List class fill method
- Using the List class tabulate method
I hope this Scala List class tutorial has been helpful.
How do I create a List in Scala?的更多相关文章
- Scala on Visual Studio Code
Download and install Scala Download a scala installation package from here. Then install it. Linux s ...
- Scala语言简介和开发环境配置
Scala语言的简介和开发环境搭建 Scala是一门结合了面向对象特征和函数式编程特征的语言,它是一个创新的编程语言产品.Scala可以做脚本(就像shell脚本一样),可以做服务端编程语言,可以写数 ...
- 在IntelliJ IDEA中创建和运行java/scala/spark程序
本文将分两部分来介绍如何在IntelliJ IDEA中运行Java/Scala/Spark程序: 基本概念介绍 在IntelliJ IDEA中创建和运行java/scala/spark程序 基本概念介 ...
- Spark SQL Example
Spark SQL Example This example demonstrates how to use sqlContext.sql to create and load a table ...
- Django学习(2)数据宝库
数据库是一所大宝库,藏着各种宝贝.一个没有数据库的网站,功能有限.在Django中,支持的数据库有以下四种: SQLite3 MySQL PostgreSQL Oracle 其中SQLite3为Dja ...
- Akka-Cluster(0)- 分布式应用开发的一些想法
当我初接触akka-cluster的时候,我有一个梦想,希望能充分利用actor自由分布.独立运行的特性实现某种分布式程序.这种程序的计算任务可以进行人为的分割后再把细分的任务分派给分布在多个服务器上 ...
- learning scala akka actorySystem create and close
package com.example import akka.actor.ActorSystem import scala.util.{Failure, Success} import scala. ...
- [lean scala]|How to create a SBT project with Intellij IDEA
this article show you how to create a SBT project with IDEA. prerequisite: 1.JDK8 2.Scala 2.11.8 3.I ...
- learning scala How To Create Variable Argument Function - varargs :_ *
Scala collection such as List or Sequence or even an Array to variable argument function using the s ...
随机推荐
- awk备忘
awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各 ...
- 微信小程序基于scroll-view实现锚点定位
代码地址如下:http://www.demodashi.com/demo/14009.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
- maven 发布到仓库
到nexus中看到的仓库地址 <distributionManagement> <repository> <id>releases</id> <n ...
- js ie下有效 showModalDialog 、showModelessDialog
<input type="button" value="打开选择输入框"/> <script type="text/javascri ...
- AbatorForEclipse插件使用总结
AbatorForEclipse是IbatorForEclipse之前的一个老版插件.插件装好后,我们来使用看看:1,新建一个工程AbatorTest,点击右键,新建一个abatorConfig.xm ...
- PL/SQL配置oracle客户端,登录远程数据库配置
本地未安装Oracle数据库,但又想使用PL/SQL连接服务器端的数据库. 1.新建NETWORK文件夹, 在该文件夹下新建ADMIN文件夹, 在该文件夹下新建tnsnames.ora文件(拷贝下面的 ...
- 理解WEB标准
WEB标准不是某一个标准,而是一系列标准的集合.网页主要由三部分组成:结构(Structure).表现(Presentation)和行为 (Behavior).对应的标准也分三方面:结构化标准语言主要 ...
- 马老师 Linux基础入门
总线(Bus)是计算机各种功能部件之间传送信息的公共通信干线,它是由导线组成的传输线束, 按照计算机所传输的信息种类,计算机的总线可以划分为数据总线.地址总线和控制总线,分别用来传输数据.数据地址和控 ...
- eclipse 无代码自动提示
http://blog.csdn.net/wgw335363240/article/details/6235427eclipse 无代码提示,显示No Default Proposals,Conten ...
- OOAD和UML
ooad: object oriented analysis designer 又有两个分支: ooa(object oriented analysis):what to do ood(object ...