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;否則為false 。 false ,否則。
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中串聯列表的方法和運算符,
::: operator [appends the list]
:::運算符 [追加列表]
List.:::() method [prepends the list]
List。:: :()方法 [準備列表]
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.
它返回串接列表1和List2列表清單 。
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