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 classtabulate method

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?的更多相关文章

  1. Scala on Visual Studio Code

    Download and install Scala Download a scala installation package from here. Then install it. Linux s ...

  2. Scala语言简介和开发环境配置

    Scala语言的简介和开发环境搭建 Scala是一门结合了面向对象特征和函数式编程特征的语言,它是一个创新的编程语言产品.Scala可以做脚本(就像shell脚本一样),可以做服务端编程语言,可以写数 ...

  3. 在IntelliJ IDEA中创建和运行java/scala/spark程序

    本文将分两部分来介绍如何在IntelliJ IDEA中运行Java/Scala/Spark程序: 基本概念介绍 在IntelliJ IDEA中创建和运行java/scala/spark程序 基本概念介 ...

  4. Spark SQL Example

     Spark SQL Example This example demonstrates how to use sqlContext.sql to create and load a table ...

  5. Django学习(2)数据宝库

    数据库是一所大宝库,藏着各种宝贝.一个没有数据库的网站,功能有限.在Django中,支持的数据库有以下四种: SQLite3 MySQL PostgreSQL Oracle 其中SQLite3为Dja ...

  6. Akka-Cluster(0)- 分布式应用开发的一些想法

    当我初接触akka-cluster的时候,我有一个梦想,希望能充分利用actor自由分布.独立运行的特性实现某种分布式程序.这种程序的计算任务可以进行人为的分割后再把细分的任务分派给分布在多个服务器上 ...

  7. learning scala akka actorySystem create and close

    package com.example import akka.actor.ActorSystem import scala.util.{Failure, Success} import scala. ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. Loadrunner 怎么将response的数据下载下来

    我们请求url时候会遇见返回的数据格式为json格式.但是怎么显示到reply页面呢?其实有一个参数需要设置成0 web_url("GetRequestData", "U ...

  2. 基础url、相对url、绝对url

    #基础url.相对url.绝对url #HTTP权威指南 34页 •绝对URL:包含了访问资源所需的全部信息,如:http://www.163.com/new.html •相对URL:去除基础的部分, ...

  3. CreateThread、_beginthreadex和AfxBeginThread .

    创建线程好几个函数可以使用,可是它们有什么区别,适用于什么情况呢?参考了一些资料,写得都挺好的,这里做一些摘抄和整合. [参考1]CreateThread, AfxBeginThread,_begin ...

  4. 赵雅智_android多线程下载带进度条

    progressBar说明 在某些操作的进度中的可视指示器,为用户呈现操作的进度,还它有一个次要的进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度. 一个进度条也可不确定其进度.在不确定模式下, ...

  5. 【laravel5.4】引入自定义类库+卸载已有的自定义库(以引入钉钉应用为例)composer dumpautoload -o

    本文之前,首先感谢: Azeroth_Yang  传送门:https://blog.csdn.net/zwrj1130/article/details/73467320 强烈建议引入的类 都是含有命名 ...

  6. HDUOJ----A Computer Graphics Problem

    A Computer Graphics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  7. eclipse 在weblogic部署的工程项目开启远程调试remote config eclipse远程调试配置

    确认你的工程在weblogic中跑的起来,然后再结合eclipse debug配置+java debug运行模式搞个调试. 工程能跑起来没问题后,先在eclipse中,点击debug图标 然后点击De ...

  8. 利用recv和readn函数实现readline函数

    在前面的文章中,我们为了避免粘包问题,实现了一个readn函数读取固定字节的数据.如果应用层协议的各字段长度固定,用readn来读是非常方便 的.例如设计一种客户端上传文件的协议,规定前12字节表示文 ...

  9. Linux内核中锁机制之原子操作、自旋锁

    很多人会问这样的问题,Linux内核中提供了各式各样的同步锁机制到底有何作用?追根到底其实是由于操作系统中存在多进程对共享资源的并发访问,从而引起了进程间的竞态.这其中包括了我们所熟知的SMP系统,多 ...

  10. win7 64 python2 xgboost安装

    综述: 首先,关于xgboost是啥,可以看这一篇:机器学习(四)--- 从gbdt到xgboost 安装Python3 环境下的xgboost 可以通过pip install , 在网址中下载对应版 ...