在Scala中列出| 关于Scala列表的完整教程

Scala | 清单 (Scala | List)

List in Scala is a collection that stores data in the form of a liked-list. The list is an immutable collection which means the elements of a list cannot be altered after the list is created. Lists are flexible with data types, i.e. they can store elements of multiple data types.

Scala中的List是一个集合,以喜欢列表的形式存储数据。 该列表是一个不可变的集合,这意味着创建列表后不能更改列表的元素。 列表具有灵活的数据类型,即它们可以存储多种数据类型的元素。

Example:

例:

// this is a valid list in Scala
List(34, "Includehelp.com", 12.42)

Scala also gives its programmer to create an alterable list i.e. a mutable list using the ListBuffer class which is defined in the scala.collection.mutable package. There are a lot of utility methods defined for Lists to help the proper functioning of the List in Scala.

Scala还允许其程序员使用在scala.collection.mutable包中定义的ListBuffer类创建一个可变列表,即可变列表。 为List定义了许多实用程序方法,以帮助Scala中的List正常运行。

Defining a List in Scala:

在Scala中定义列表:

There are multiple syntaxes that can be used to define a list in Scala. Let's see each of them,

可使用多种语法在Scala中定义列表。 让我们看看他们每个人,

Method 1:

方法1:

val list_name: List[type] = List(element1, element2, element3...)

The list declared using this method will have all elements of the same data type.

使用此方法声明的列表将具有相同数据类型的所有元素。

Method 2:

方法2:

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

Scala中的列表示例 (Examples of List in Scala)

Let's explore some valid List in Scala,

让我们探索Scala中的一些有效列表,

  • Empty List,

    空列表,

    List()
    
    
  • List of values of same type,

    相同类型的值列表,

    List(3, 65, 12, 1451, 99)
    
    
  • List of values of multiple types,

    多种类型的值列表,

    List(1013, "Maths", 97.8)
    
    
  • 2-D list i.e ListofList,

    二维列表,即ListofList,

    List(List(123, 41341), List(1, 46))
    
    

These were various types of List in Scala. Now, let's head to some action and create a list.

这些是Scala中各种类型的List。 现在,让我们开始一些操作并创建一个列表。

Program 1: Program to create a list of numbers in Scala.

程序1:在Scala中创建数字列表的程序。

object MyClass {
def main(args: Array[String]) {
// creating List of Integer values 
val number_list : List[Integer] = List(10, 25, 17, 93, 77)
// printing List 
println("The list of numbers is "+ number_list)
}
}

Output:

输出:

The list of numbers is List(10, 25, 17, 93, 77)

Program 2: Program to create a 2-D mixed List.

程序2:创建二维混合列表的程序。

object MyClass {
def main(args: Array[String]) {
// creating 2D List  
val list_2d = List(List(1, "C++", 1979), List(2, "JavaScript", 1995), List(3, "Scala", 2004))
// printing List 
println("The 2D list with dates of invension of programming languages is "+ list_2d)
}
}

Output:

输出:

The 2D list with dates of invension of programming languages is List(List(1, C++, 1979), List(2, JavaScript, 1995), List(3, Scala, 2004))

After learning how to create a List in Scala, let's go through some operations on List.

学习了如何在Scala中创建列表后 ,让我们对List进行一些操作。

1)isEmpty方法 (1) isEmpty Method)

It is used to check an empty list, it is an important function as prior to performing any of the complex operations on Scala, we need to make sure that the list has elements to perform operations on.

它用于检查一个空列表,它是一项重要功能,因为在对Scala执行任何复杂操作之前,我们需要确保列表中包含要对其执行操作的元素。

Syntax:

句法:

list_name.isEmpty

This method returns a boolean value, true if the list is empty; false, otherwise.

此方法返回一个布尔值,如果列表为空,则为true;否则为falsefalse ,否则。

Program 3: Program to show the working of isEmpty

程序3:该程序显示isEmpty的工作

object MyClass {
def main(args: Array[String]) {
// creating  List  
val list1 = List(12, 43, 99)
val list2 = List()
// printing List's 
println("List1 : "+ list1)
println("List2 : "+ list2)
// check if the list isEmpty...
println("list1.isEmpty = "+ list1.isEmpty)
println("list2.isEmpty = "+ list2.isEmpty)        
}
}

Output:

输出:

List1 : List(12, 43, 99)
List2 : List()
list1.isEmpty = false
list2.isEmpty = true

2)头法 (2) head Method)

It is used to het the first element of the List.

它用于提示列表的第一个元素。

Syntax:

句法:

List_name.head

This method returns a value, the first element of List.

此方法返回一个值,即List的第一个元素。

3)尾法 (3) tail Method)

It is used to return all elements of the List except the first element. All the elements returned will be in the form of a list.

它用于返回列表中除第一个元素外的所有元素。 返回的所有元素将以列表的形式。

Syntax:

句法:

List_name.tail

This method returns a list, all elements except the first.

此方法返回一个列表,除第一个元素外的所有元素。

Program 4: Program to find the head and tail of a List

程序4:查找列表头和尾的程序

object MyClass {
def main(args: Array[String]) {
// creating  List  
val nations = List("India", "America", "Japan", "Russia")
//Printing the List 
println("The List is : " + nations)
//Printing head of the List
println("List.head : " + nations.head)
//Printing tail of the List
println("List.tail : " + nations.tail)
}
}

Output:

输出:

The List is : List(India, America, Japan, Russia)
List.head : India
List.tail : List(America, Japan, Russia)

Scala中的串联列表 (Concatenate Lists in Scala)

Concatenation is the process of merging to lists to one. You can append list (add a list at the end of another list) or prepend the list (adding a list at the start of another list).

串联是将列表合并为一个的过程。 您可以追加列表 (在另一个列表的末尾添加一个列表)或在列表之前 (在另一个列表的末尾添加一个列表)。

The methods and operators to concatenate lists in Scala,

在Scala中串联列表的方法和运算符,

  1. ::: operator [appends the list]

    :::运算符 [追加列表]

  2. List.:::() method [prepends the list]

    List。:: :()方法 [准备列表]

  3. List.concat() method [appends the list]

    List.concat()方法 [追加列表]

4)使用:::运算符连接列表 (4) Concatenating Lists using the ::: operator)

You can append lists in Scala using the ::: operator.

您可以使用:::运算符在Scala中附加列表。

Syntax:

句法:

list1 ::: list2 

It returns a list which is concatenated list of list1 and list2.

它返回串接列表1List2列表清单

Program 5: Program to concatenate two lists of strings to one using ::: operator

程序5:使用:::运算符将两个字符串列表连接为一个字符串的程序

object MyClass {
def main(args: Array[String]) {
// creating  Lists  
val bikes1 = List("RE ThunderBird 350" , "YRF R3")
val bikes2 = List("HD Iron 883" , "BMW S1000RR")
//Printing the Lists 
println("List Bike1 : " + bikes1)
println("List Bike2 : " + bikes2)
//concatenating List 
val conList = bikes1 ::: bikes2
println("Concatenated List : " + conList)
}
}

Output:

输出:

List Bike1 : List(RE ThunderBird 350, YRF R3)
List Bike2 : List(HD Iron 883, BMW S1000RR)
Concatenated List : List(RE ThunderBird 350, YRF R3, HD Iron 883, BMW S1000RR)

5)使用List。:: :()方法连接List (5) Concatenating List using List .:::() method)

You can prepend list in Scala using the List .:::() method.

您可以使用List 。:: :()方法在Scala中添加列表。

Syntax:

句法:

list1.:::(list2)

It returns a list which is prepending list2 before list1.

它返回一个列表 ,该列表list1之前位于list2之前。

Program 6: Program to concatenate two lists of numbers using List .:::() method

程序6:使用List。:: :()方法连接两个数字列表的程序

object MyClass {
def main(args: Array[String]) {
// creating  Lists  
val numList1 = List(4, 9, 21, 75)
val numList2 = List(1, 7, 12, 25)
//Printing the Lists 
println("List numList1 : " + numList1)
println("List numList2 : " + numList2)
//concatenating List 
val numList = numList1.:::(numList2)
println("Concatenated List : " + numList)
}
}

Output:

输出:

List numList1 : List(4, 9, 21, 75)
List numList2 : List(1, 7, 12, 25)
Concatenated List : List(1, 7, 12, 25, 4, 9, 21, 75)

6)使用List.concat()方法连接List (6) Concatenating List using List.concat() method)

You can concatenate list in Scala using List.concat() method.

您可以使用List.concat()方法在Scala中串联列表。

Syntax:

句法:

List.concat(list1, list2)

It returns the concatenated list.

它返回连接的列表。

Program 7: Program to concatenate two lists of type using the concat() method

程序7:使用concat()方法连接两个类型列表的程序

object MyClass {
def main(args: Array[String]) {
// creating  Lists  
val numList = List(4, 9, 21, 75) // number list
val stringList = List("includehelp" , "programming" , "tutorials") // String list
//Printing the Lists 
println("List numList : " + numList)
println("List stringList : " + stringList)
//concatenating List 
val concatList = List.concat(numList, stringList)
println("Concatenated List : " + concatList)
}
}

Output:

输出:

List numList : List(4, 9, 21, 75)
List stringList : List(includehelp, programming, tutorials)
Concatenated List : List(4, 9, 21, 75, includehelp, programming, tutorials)

7)使用填充方法创建列表 (7) Creating List using fill method)

You can create a list of multiple copies of elements using the List.fill() method.

您可以使用List.fill()方法创建元素的多个副本的列表。

Syntax:

句法:

List.fill(repeat_times)(element)

It returns a list with elements copied multiple times.

它返回一个列表,其中包含多次复制的元素。

Program 8: Program to create a list by repeating string N times using fill() method

程序8:通过使用fill()方法重复N次字符串来创建列表的程序

object MyClass {
def main(args: Array[String]) {
// creating  List 
val newList = List.fill(5)("includehelp")
//Printing the Lists 
println("newList : " + newList)
}
}

Output:

输出:

newList : List(includehelp, includehelp, includehelp, includehelp, includehelp)

8)倒序排列 (8) Reverse order of list)

You can reverse the order of a list in Scala using the reverse method.

您可以使用reverse方法在Scala中反转列表的顺序。

Syntax:

句法:

List.reverse

It returns a list with elements in reverse order.

它返回带有相反顺序元素的列表。

Program 9: Program to reverse the given list of integers using reverse method

程序9:使用反转方法反转给定整数列表的程序

object MyClass {
def main(args: Array[String]) {
// creating  Lists  
val newList = 12 :: (54 :: (99 :: (1210 :: Nil)))
//Printing the List 
println("newList : " + newList)
//Reversing List 
val revList = newList.reverse
// printing reverse list
println("reversed List : " + revList)
}
}

Output:

输出:

newList : List(12, 54, 99, 1210)
reversed List : List(1210, 99, 54, 12)

9)列表 (9) Tabulating List)

You can create a list using a function that calculates for each value and adds the result to the list.

您可以使用为每个值计算并将结果添加到列表的函数来创建列表。

Syntax:

句法:

List.tabulate(repeat_times)(evaluation_function)

It returns a list which has elements in tabular form.

它返回一个包含表格形式元素的列表。

Program 10: Program to create a list using tabulate() method

程序10:使用tabulate()方法创建列表的程序

object MyClass {
def main(args: Array[String]) {
// creating  Lists  
val table5 = List.tabulate(10)(i => i*5)
//Printing the List 
println("table5 : " + table5)
// creating a 2D List 
val values = List.tabulate(5, 5)(_+_)
//Printing the List
println("values : " + values)
}
}

Output:

输出:

table5 : List(0, 5, 10, 15, 20, 25, 30, 35, 40, 45)
values : List(List(0, 1, 2, 3, 4), List(1, 2, 3, 4, 5), List(2, 3, 4, 5, 6), List(3, 4, 5, 6, 7), List(4, 5, 6, 7, 8))

This is all about lists, creations and operations of List. There are some methods that are defined in the List class of Scala to support the functioning of List. You can see them at function reference of Scala List.

这全部与List的列表, 创建和操作有关 。 Scala的List类中定义了一些方法来支持List的功能。 您可以在Scala List的功能参考中看到它们。

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540736.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MySQL单机多实例部署详解之------多实例分别定义不同的配置文件

mysql多实例安装---分别定义不同的配置文件1.安装MySQL需要的依赖包[rootMySQL ~]# yum install ncurses-devel libaio-devel cmake -y[rootMySQL ~]# rpm -qa ncurses-devel libaio-develncurses-devel-5.7-4.20090207.el6.x86_64libaio-devel-0.3.107-10.el6.x86_642.安装编译…

javascript模块_JavaScript中的模块

javascript模块JavaScript模块 (JavaScript Modules) One of the key features of programming fundamentals is breaking down your code into fragments. These fragments depending on its functionality have been coined various terms like functions, components, modul…

在mac上安装Docker

1.进入一下地址进行下载docker https://download.docker.com/mac/stable/Docker.dmg 进入后进行下载后进行安装 2.将其拖动到Appliaction中即可 3.第一打开会有一个这样的欢迎页面 3.检查是否安装完成 出现上图所示标示安装完成了

composer 检查镜像_检查N元树中的镜像

composer 检查镜像Problem statement: 问题陈述: Given two n-ary trees, the task is to check if they are mirrors of each other or not. 给定两个n元树,任务是检查它们是否互为镜像。 Note: you may assume that root of both the given tree as …

浪潮各机型前面板指示灯含义

NF560D2 NF3020M2 NF5020M3 NF5140M3 NF5212H2 NF5220 NF5224L2 NF5240M3 NF5270M3 NF5280M2 NF5280M3 NF5540M3 NF5580M3 NF8420M3 NF8520 NF8560M2 说明:转浪潮官网。

python dll 混合_Python | 条线混合图

python dll 混合In some of the cases, we need to plot a bar-line hybrid plot. This plot helps in a better understanding of dynamics as well as the relative magnitude of each point in the plot. Bar-Line Hybrid Plots are mostly used for the representation of …

测试八 赛后感受

测试八 当我打开T1的时候,就没有往下看题目了,主要是发现T1就是之前做过,而且我也看过题解的题目,接着就开始钻研,当然,也没什么好钻研的,大概思路还是知道的,再写写数据就已经很清晰…

推荐五个免费的网络安全工具

导读: 在一个完美的世界里,信息安全从业人员有无限的安全预算去做排除故障和修复安全漏洞的工作。但是,正如你将要学到的那样,你不需要无限的预算取得到高质量的产品。这里有SearchSecurity.com网站专家Michael Cobb推荐的五个免费…

bios部署模式审核模式_BIOS的完整形式是什么?

bios部署模式审核模式BIOS:基本输入输出系统 (BIOS: Basic Input Output System) BIOS is an abbreviation of the Basic Input Output System. In the beginning, when you first set on your computer, the first software which starts run by the computer is &…

day04-装饰器

一、装饰器定义 1)装饰器:本质是函数。 2)功能:用来装饰其他函数,顾名思义就是,为其他的函数添加附件功能的。 二、原则 1)不能修改被装饰函数的源代码 2)不能修改被装饰函数的调用方…

c 语言bool 类型数据_C ++中的bool数据类型

c 语言bool 类型数据In C programming language, to deal with the Boolean values – C added the feature of the bool data type. A bool variable stores either true (1) or false (0) values. 在C 编程语言中,为了处理布尔值– C 添加了bool数据类型的功能 。…

C ++中的std :: binary_search()

binary_search()作为STL函数 (binary_search() as a STL function) Syntax: 句法: bool binary_search (ForwardIterator first, ForwardIterator last, const T& value);Where, 哪里, ForwardIterator first iterator to start of the range For…

HNUSTOJ-1437 无题

1437: 无题 时间限制: 1 Sec 内存限制: 128 MB提交: 268 解决: 45[提交][状态][讨论版]题目描述 tc在玩一个很无聊的游戏:每一次电脑都会给一个长度不超过10^5的字符串,tc每次都从第一个字符开始,如果找到两个相邻相一样的字符,…

凯撒密码pythin密码_凯撒密码术

凯撒密码pythin密码Caesar cipher is one of the well-known techniques used for encrypting the data. Although not widely used due to its simplicity and being more prone to be cracked by any outsider, still this cipher holds much value as it is amongst the fir…

MultiQC使用指导

MultiQC使用指导 官网资料文献:MultiQC --- summarize analysis results for multiple tools and samples in a single report参考资料一: 整合 fastq 质控结果的工具 简介 MultiQC 是一个基于Python的模块, 用于整合其它软件的报告结果, 目前支持以下软…

FYFG的完整形式是什么?

FYFG:对您的未来指导 (FYFG: For Your Future Guidance) FYFG is an abbreviation of "For Your Future Guidance". FYFG是“ For your Future Guidance”的缩写 。 It is an expression, which is commonly used in the Gmail platform. It is also wr…

WorkerMan 入门学习之(二)基础教程-Connection类的使用

一、TcpConnection类 的使用 1、简单的TCP测试 Server.php <?php require_once __DIR__./Workerman/Autoloader.php; use Workerman\Worker; $worker new Worker(websocket://0.0.0.0:80);// 连接回调 $worker->onConnect function ($connection){echo "connecti…

kotlin获取属性_Kotlin程序获取系统名称

kotlin获取属性The task is to get the system name. 任务是获取系统名称。 package com.includehelpimport java.net.InetAddress/*** Function for System Name*/fun getSystemName(): String? {return try {InetAddress.getLocalHost().hostName} catch (E: Exception) {S…

71文件类型

1.kit类型 标准的SeaJs模块文件类型&#xff0c;直接对外暴露方法。 2.units类型 依赖pageJob&#xff0c;对外暴露一个名字&#xff0c;pageJob依赖暴露的名字对模块进行初始化&#xff0c;在pageJob内部逻辑自动执行init方法&#xff1b; 由于没有对外暴露方法&#xff0c;只…

ruby 生成哈希值_哈希 Ruby中的运算符

ruby 生成哈希值In the last article, we have seen how we can carry out a comparison between two hash objects with the help of "" operator? "" method is a public instance method defined in Ruby’s library. 在上一篇文章中&#xff0c;我们看…