Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Definition and usage of List in Scala

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report