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 ...
随机推荐
- KeyBoardUtils.java——android键盘工具类
package com.xdsjs.save.utils; import android.content.Context; import android.view.inputmethod.InputM ...
- vue 数据劫持 响应式原理 Observer Dep Watcher
1.vue响应式原理流程图概览 2.具体流程 (1)vue示例初始化(源码位于instance/index.js) import { initMixin } from './init' import ...
- 普通spring jsp+mybatis项目修改为springboot + jsp +mybatis项目
概述 由于公司决定使用spring cloud,但是公司积累了大量的普通的jsp项目,老的项目直接全部修改为springboot成本过高,周期比较长,而且公司业务正在快速拓展,所以需要把之前的老项目修 ...
- INFO ipc.Client:Retrying connect to server 9000
hadoop使用bin/start_all.sh命令之后,使用jps发现datanode无法启动 This problem comes when Datanode daemon on the syst ...
- Oracle用同义词可以不使用用户名.表名查询表 Oracle 创建synonym
一般在PL/SQL下,Oracle 用户查询其他用户的表一定要使用user.table格式才能查询,每次用户名.表名嫌麻烦的话.可以使用同义词,意思将user.table访问格式改成table格式即可 ...
- Sublime Text 2搭建Go开发环境(Windows)
转自:http://blog.csdn.net/love_se/article/details/7754274 下载packcontrol包地址:http://www.imjeff.cn/blog/6 ...
- JMeter学习笔记--JMeter常用测试元件
JMeter测试计划有一个被称为“函数测试模式”的选项,如果被选择,它会使Jmeter记录来自服务器返回的每个取样的数据.如果你在测试监听器中选择一个文件,这个数据将被写入文件.如果你尝试一个较小的测 ...
- 【转】其他人的BUG
在软件行业,经常看到有的公司管理让一个人修补另一个人代码里的BUG.有时候有人写了一段代码,扔出来不管了,然后公司管理让其他工程师来修复它.我想告诉你们,这种方法会很失败. 首先,让一个人修复另一个人 ...
- linux内存碎片防治技术
Linux kernel组织管理物理内存的方式是buddy system(伙伴系统),而物理内存碎片正式buddy system的弱点之一,为了预防以及解决碎片问题,kernel采取了一些实用技术,这 ...
- VC6.0编译DLL,使用VS2010调用问题及解决方法
1.做驱动的时候.做应用程序须要和驱动通信,必须建立一个DLL. 2.由于客户使用版本号太低,须要使用到VC6.0编写DLL 3.在VC6.0上编写DLL的时候,导出的函数名会出现和原函数名不正确,导 ...