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 ...
随机推荐
- C语言printf
1.调用格式为 printf("<格式化字符串>", <参量表>); 其中格式化字符串包括两部分内容: 一部分是正常字符, 这些字符将按原样输出; 另一部 ...
- Linux 系统文件类型
在Linux中只有一下7种文件类型 1.普通文件(-表示,可用ls -l查看) 2.目录(d)[在linux中,任何东西都被看成文件,外设备也看成文件,注意是任何东西] 3.字符设备文件(c) 4.块 ...
- linux too many open files报错
修改方法:vi /etc/security/limits.conf 增加一行,如下: * - nofile 65535 修改vi /etc/ssh/sshd ...
- Python pycharm(windows版本)部署spark环境
一 部署本地spark环境 1.1 安装好JDK 下载并安装好jdk1.7,配置完环境变量. 1.2 Spark环境变量配置 去http://spark.apache.o ...
- iOS AppIcon尺寸和上传ITunes构建版本尺寸和iPhone屏幕尺寸
避免忘记. 记录一下 App Icon: 29X2940X4058X5876X7687X8780X80120X120152X152167X167180X180 ITunes构建版本: 1242 x 2 ...
- nginx error: upstream prematurely closed connection while reading response header from upstream
本篇文章由:http://xinpure.com/nginx-error-upstream-prematurely-closed-connection-while-reading-response-h ...
- IntelliJ Idea解决Could not autowire. No beans of 'xxxx' type found的错误提示
本文转自:http://blog.csdn.net/u012453843/article/details/54906905 1.问题描述 在Idea的spring工程里,经常会遇到Could not ...
- JavaScript:用JS实现加载页面前弹出模态框
用JS实现加载页面前弹出模态框 主要的JavaScript 代码是: <script> //加载模态框 $('#myModal').modal(); $(document).ready(f ...
- windows平台安装redis服务
有时候我们需要在windows平台上使用redis作为缓存服务,这个时候就需要将redis安装为window服务. 现在将我安装过程记录下来,方便自己或者有类似需求的同学参考.主要是参考网上现有的例子 ...
- 安卓请求网络错误 直接在main Thread 进行网络操作出现maintreamexception
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites ...