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. eclipse/STS 切换目录视图

  2. eclipse 将javaWeb项目转化成maven项目

      eclipse 将javaWeb项目转化成maven项目 CreateTime--2018年4月18日16:04:18 Author:Marydon 1.首先,maven项目的标准目录 2.web ...

  3. 基于cancel的不全然恢复

    实验:基于cancel的不全然恢复 实验环境查看 lsnrctl status select open_mode from v$database;   --监听与数据库状态 show paramete ...

  4. Oracle使用——PLSQL的中文乱码显示全是问号

    问题 这两天刚将PLSQL与Oracle配置好,但是在PLSQL中插入数据时.出现一个问题,PLSQL中的表里无法显示中文,中文无法保存.无法输出.中文在表中显示问号.如图: 原因 经过一番查证,发现 ...

  5. TCP/IP协议栈--IP首部选项字段的分析

    IP输入函数(ipintr)将在验证分组格式(检验和,长度等)之后.确定分组是否到达目的地之前,对选项进行处理. 这表明分组所 遇到的每一个路由器以及终于的目的主机都对要分组的选项进行处理. IP分组 ...

  6. spring.net aop 讲解

    spring.net aop几个术语: 切面:针对类 切点:针对方法 object.xml <?xml version="1.0" encoding="utf-8& ...

  7. 微信小程序:input输入框和form表单几种传值和取值方式

    1.传值:index下标传值.页面navigator传值 1.index下标 实现方式是:data-index="{{index}}"挖坑及e.currentTarget.data ...

  8. MyEcplise安装Freemarker插件(支持.ftl文件)

    1.下载插件:http://sourceforge.net/projects/freemarker-ide/?source=typ_redirect 2.下载freemarker-2.3.19.jar ...

  9. LinkedHashMap插入无序且链式操作

    Iterator<Entry<Integer, Integer>> ite=lhmap.entrySet().iterator(); ite.next(); ite.remov ...

  10. HDUOJ----4004The Frog's Games(二分+简单贪心)

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...