当前位置: 首页 > 工具软件 > Scala Check > 使用案例 >

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

费辰阳
2023-12-01

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

 类似资料: