8.30. Scala List

发布时间 : 2025-10-25 13:35:15 UTC      

Page Views: 10 views

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 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) ) 

The two basic units for constructing a list are Nil and ::

Nil can also be represented as an empty list.

The above example can be written as follows:

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 #

The Scala list has three basic operations:

  • 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

Any operation on the Scala list can be expressed using these three basic operations. Examples are as follows:

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) } } 

Execute the above code, and the output is as follows:

$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 #

You can use it. ::: 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 ) } } 

Execute the above code, and the output is as follows:

$ 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() #

We can use it. 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 ) } } 

Execute the above code, and the output is as follows:

$ 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() #

The List.tabulate() method is to create a list with a given function.

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:

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。