Lists
-
Type:
List[T]
-
Construction:
val fruit = List("apple", "banana", "orange") val nums = 1 :: 2 :: Nil
-
Decomposition
val apple = fruit.head
- The most useful functions:
nums.head
,nums.tail
,nums.isEmpty
nums.length
nums.last
,nums.init
: the opposite ofnums.head
,nums.tail
nums.take(n)
: get firstn
nodesnums.drop(n)
: get the rest after drop the firstn
nodes.nums(i)
: get the i<sup>th</sup> node. It equals tonums.apply(i)
- The most useful functions:
-
Creating a new list
xs ::: ys
orxs ++ ys
: concat 2 lists, and return a new onexs.reverse
: return a reversedxs
xs.update(i, x)
: update the i<sup>th</sup> node byx
, and return a new one
-
Finding a node
xs.indexOf(x)
: return the index ofx
inxs
.-1
if not found.xs.contains(x)
: Same asxs.indexOf(x) >= 0