Scala lists are similar to arrays in that all elements are of the same type,but they are also different: lists are immutable, values cannot be changed once they are defined, and lists have a recursive structure (that is, linkedtable structures) while arrays are not.
The element type T of the list can be written as The two basic units for constructing a list are The above example can be written as follows: The Scala list has three basic operations: Any operation on the Scala list can be expressed using these three basic operations. Examples are as follows: Execute the above code, and the output is as follows: You can use it. Execute the above code, and the output is as follows: We can use it. Execute the above code, and the output is as follows: The The first parameter of the method is the number of elements, which can be two-dimensional, and the second parameter is the specified function. We calculate the result through the specified function and insert the returned value into the list. The starting value is 0. The example is as follows:
List[T]
. For example,several types of lists are listed below: 8.30.1. Example #
// String List
val site: List[String] = List("Runoob", "Google", "Baidu")
// Integer List
val nums: List[Int] = List(1, 2, 3, 4)
// Empty List
val empty: List[Nothing] = List()
// 2D List
val dim: List[List[Int]] =
List(
List(1, 0, 0),
List(0, 1, 0),
List(0, 0, 1)
)
Nil
and
::
Nil
can also be represented as an empty list. 8.30.2. Example #
// String List
val site = "Runoob" :: ("Google" :: ("Baidu" :: Nil))
// Integer List
val nums = 1 :: (2 :: (3 :: (4 :: Nil)))
// Empty List
val empty = Nil
// 2D List
val dim = (1 :: (0 :: (0 :: Nil))) ::
(0 :: (1 :: (0 :: Nil))) ::
(0 :: (0 :: (1 :: Nil))) :: Nil
List basic operation #
head
return to the first element of the list
tail
returns a list of elements other than the first element
isEmpty
returns when the list is empty
true
8.30.3. Example #
//String List
Object Test{
Def main (args: Array [String]){
Val site="Runoob": ("Google": ("Baidu": Nil))
Val nums=Nil
Println ("The first website is:"+site. head)
Println ("The last website is:"+site. tail)
Println ("Check if the list site is empty:"+site. isEmpty)
Println ("Check if nums is empty:"+nums. isEmpty)
}
}
$vim Test.scala
$scala Test.scala
The first website is: Runoob
The last website is: List (Google, Baidu)
Check if the list site is empty: false
Check if nums is empty: true
Connection list #
:::
Operator or
List.:::()
method or
List.concat()
method to join two or more lists. Examples are as follows: 8.30.4. Example #
object Test {
def main(args: Array[String]) {
val site1 = "Runoob" :: ("Google" :: ("Baidu" :: Nil))
val site2 = "Facebook" :: ("Taobao" :: Nil)
// Using the ::: operator
var fruit = site1 ::: site2
println( "site1 ::: site2 : " + fruit )
// Using List.:::() method
fruit = site1.:::(site2)
println( "site1.:::(site2) : " + fruit )
// Using concat ethod
fruit = List.concat(site1, site2)
println( "List.concat(site1, site2) : " + fruit )
}
}
$ vim Test.scala
$ scala Test.scala
site1 ::: site2 : List(Runoob, Google, Baidu, Facebook, Taobao)
site1.:::(site2) : List(Facebook, Taobao, Runoob, Google, Baidu)
List.concat(site1, site2) : List(Runoob, Google, Baidu, Facebook, Taobao)
List.fill()
#
List.fill()
method to create a list of elements with a specified number of repeats: 8.30.5. Example #
object Test {
def main(args: Array[String]) {
val site = List.fill(3)("Runoob") // Repeat Runoob 3 times
println( "site : " + site )
val num = List.fill(10)(2) // Repeat element 2, 10 times
println( "num : " + num )
}
}
$ vim Test.scala
$ scala Test.scala
site : List(Runoob, Runoob, Runoob)
num : List(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
List.tabulate()
#
List.tabulate()
method is to create a list with a given function.