Scala List


Release date:2023-11-18 Update date:2023-11-21 Editor:admin View counts:167

Label:

Scala List

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:

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:

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:

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:

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:

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:

Powered by TorCMS (https://github.com/bukun/TorCMS).