scala中创建时间序列_在Scala中创建列表

scala中创建时间序列

在Scala中列出 (List in Scala)

List is a collection of immutable data of the same type. It represents a LinkedList in Scala.

列表是相同类型的不可变数据的集合。 它代表Scala中的LinkedList。

在Scala中创建列表 (Creating List in Scala)

There are multiple ways to create a list. They are,

有多种创建列表的方法。 他们是,

  1. General Java-style programming method

    通用Java风格的编程方法

  2. Using fill method

    使用填充方法

  3. Using range method

    使用范围法

  4. Using tabulate method

    使用制表法

  5. Using lisp style of programming

    使用Lisp风格的编程

1)以Java风格的编程方法创建列表 (1) Creating List in Java-style programming method)

The general and most popular method to create a list in initializing the elements inside a curly bracket the same style is used to create the list in java, hence it is called the Java-style.

在大括号中初始化元素时,创建列表的一般方法和最受欢迎的方法是使用相同的样式在Java中创建列表,因此将其称为Java样式。

Syntax:

句法:

    val list_name = List(element1, element2,...) 

Example:

例:

object MyObject {
    def main(args: Array[String]) {
        println("Creating a List in Java Style")
        val mylist = List(10, 20, 10, 40, 10, 20, 90, 70)
        println("Element of the list:\n" + mylist)
    }
}

Output

输出量

Creating a List in Java Style
Element of the list:
List(10, 20, 10, 40, 10, 20, 90, 70)

2)使用fill()方法在Scala中创建列表 (2) Creating a List in Scala using fill() method)

If you want to create a List of multiple elements with the same data, we will use the fill() method.

如果要创建具有相同数据的多个元素的列表,我们将使用fill()方法。

Syntax:

句法:

    val list_name = List.fill(count)(element)

Here,

这里,

  • count is the number of elements of the List.

    count是列表中元素的数量。

  • element is the element that is repeated in the list.

    element是在列表中重复的元素。

Example:

例:

object MyObject {    
    def main(args: Array[String]) {
        println("Creating a List using fill method ")
        val mylist = List.fill(5)("includehelp")
        println("Element of the list:\n" + mylist)
    }
}

Output

输出量

Creating a List using fill method 
Element of the list:
List(includehelp, includehelp, includehelp, includehelp, includehelp)

3)使用range()方法在Scala中创建列表 (3) Creating a List in Scala using range() method)

If you want to create a List with elements within a given range, we will use the range() method.

如果要创建具有给定范围内的元素的列表,我们将使用range()方法。

Syntax:

句法:

    val list_name = List.range(start, end, step)

Here,

这里,

  • start: beginning value of the range.

    start :范围的起始值。

  • end: end value of the range.

    end :范围的结束值。

  • step: the step count i.e. element to be skipped in the List.

    step :步数,即要在列表中跳过的元素。

Example:

例:

object MyObject {    
    def main(args: Array[String]) {
        println("Creating a List using range method ")
        val mylist = List.range(10, 50, 5)
        println("Element of the list:\n" + mylist)
    }
}

Output

输出量

Creating a List using range method 
Element of the list:
List(10, 15, 20, 25, 30, 35, 40, 45)

4)使用tabulate()方法在Scala中创建列表 (4) Creating a List in Scala using tabulate() method)

If you want to create a List with elements that are created passing a set of values to a function.

如果要使用创建的元素创建列表,则将一组值传递给函数。

Syntax:

句法:

    val list_name = List.tabulate(count)(function)

Here,

这里,

  • count is count from 0 to count passed to function.

    count是从0到传递给函数的count的计数。

  • function on which the count elements will be passed.

    传递计数元素的函数 。

Example:

例:

object MyObject {    
    def main(args: Array[String]) {
        println("Creating a List using tabulate method")
        val mylist = List.tabulate(10)(n => n+1)
        println("Elements of the list:\n" + mylist)
    }
}

Output

输出量

Creating a List using tabulate method
Elements of the list:
List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

5)以Lisp样式在Scala中创建列表 (5) Creating a List in Scala in Lisp style)

If you want to create a List using Lisp style programming using ::.

如果要使用Lisp样式编程通过::创建列表。

Syntax:

句法:

    val list_name = element1 :: element2 :: Nil

Nil here is used to end the list.

这里的Nil用于结束列表。

Example:

例:

object MyObject {
    def main(args: Array[String]) {
        println("Creating a List using lisp style ")
        val mylist = "BMW S1000 RR" :: "Thunderbird 350" :: "Iron 883" :: Nil
        println("Elements of the list:\n" + mylist)
    }
}

Output

输出量

Creating a List using lisp style 
Elements of the list:
List(BMW S1000 RR, Thunderbird 350, Iron 883)


翻译自: https://www.includehelp.com/scala/creating-a-list.aspx

scala中创建时间序列

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值