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.lengthnums.last,nums.init: the opposite ofnums.head,nums.tailnums.take(n): get firstnnodesnums.drop(n): get the rest after drop the firstnnodes.nums(i): get the i<sup>th</sup> node. It equals tonums.apply(i)
- The most useful functions:
-
Creating a new list
xs ::: ysorxs ++ ys: concat 2 lists, and return a new onexs.reverse: return a reversedxsxs.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 ofxinxs.-1if not found.xs.contains(x): Same asxs.indexOf(x) >= 0