Definition and usage of List in Scala
This article introduces the relevant knowledge of "the definition and usage of List in Scala". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
# Declaration
Var list = List (1, 2, 3, 4) println (list)
Empty list
Var foo = List () println (foo) var bar = Nilprintln (bar)::
The:: (two colons) operators are joined to the right, and if you want to build a list List (1, 2, 3, 4), you can actually use the following way, where Nil represents an empty list.
Scala > 1::2::3::4::Nilres6: List [Int] = List (1,2,3,4)
The list has the concept of head and tail, head returns the first element of the list, tail returns a list containing elements other than the first element, the head is an element, and the tail is still a list.
Scala > a.tailres3: List [String] = List (b, c)
# isEmpty
IsEmpty returns when the list is empty
Println (foo.head) var bar = Nilprintln (bar.isEmpty)
#: three colon
:: operator joins different lists to get new lists
Scala > val a = "a":: "b":: "c":: Nila: List [String] = List (a, b, c) scala > val b = 1::2::3::Nilb: List [Int] = List (1,2,3) scala > val c = a:::bc: List [Any] = List (a, b, c, 1,2,3) "definition and usage of List in Scala". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!