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 ...
随机推荐
- Fiddler SessionFlags
Each Session object in Fiddler contains a collection of string flags, in the Session.oFlags[] collec ...
- Spring Data MongoDB 三:基本文档查询(Query、BasicQuery)(一)
一.简单介绍 Spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一 ...
- 从 高斯 到 正态分布 到 Z分布 到 t分布
正态分布是如何被高斯推导出来的, 我感觉高斯更像是猜出了正态分布. 详见这篇文章:<正态分布的前世今生> http://songshuhui.NET/archives/76501 说一说理 ...
- java随机函数使用方法Random
import java.util.Random; public class RandomNumber{ public static void main(String[] args) { // 使用ja ...
- 在win7/8下搭建简易的无线平台
资料:http://www.cnblogs.com/KeenLeung/p/3482073.html http://www.cnblogs.com/KeenLeung/p/3481998.html 其 ...
- 更改虚拟内存(使用于win7、win8系统)
在使用电脑的过程中你肯定有这样的抱怨吧!电脑为什么越来越慢?C盘为什么越来越小?我们都非常清楚:C盘剩余空间量的大小,很大程度上决定着我们在使用电脑的过程中程序运行的速度.随着电脑软件越装越多,尽管我 ...
- NYOJ——————数的长度(斯特林公式的应用)
数的长度 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 N!阶乘是一个非常大的数,大家都知道计算公式是N!=N*(N-1)······*2*1.现在你的任务是计算出 ...
- Android学习系列(10)--App列表之拖拽ListView(上)
研究了很久的拖拽ListView的实现,受益良多,特此与尔共飨. 鉴于这部分内容网上的资料少而简陋,而具体的实现过程或许对大家才有帮助,为了详尽而不失真,我们一步一步分析,分成两篇文章. ...
- 玩转Masonry JS库来实现瀑布流Web效果
工作项目中需要制作个Mobile上的Web App的展示,方便快捷访问和评价反馈.在展示页面能看到应用展示图,点击进入Web应用.我不是前端开发者,对HTML, CSS, JS这三剑客仅仅是略知一二. ...
- 【jQuery】form表单元素序列化为json对象
序列化form表单元素为json对象: <!Doctype html> <html xmlns=http://www.w3.org/1999/xhtml> <head&g ...