在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.pswp.cn/news/540736.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540736.shtml
英文地址,請注明出處:http://en.pswp.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;我們看…