Back
Close

Introduction to Scala Part2 : Collections

Bubu
723 views

List

List creation

  • List.apply
> List.apply(1, 2, 3, 4)
res53: List[Int] = List(1, 2, 3, 4)
  • List.::
> 1 :: 2 :: 3 :: 4 :: Nil
res6: List[Int] = List(1, 2, 3, 4)
  • Nil
object Nil extends List[Nothing] with Product with Serializable

List

Some method of List

  • List.range
List.range(1, 5)
res54: List[Int] = List(1, 2, 3, 4)
  • List.fill
List.fill(5)('a')
res57: List[Char] = List(a, a, a, a, a)
List.fill(5)('a')
res57: List[Char] = List(a, a, a, a, a)
  • List.concat
> List.concat(List('a', 'b'), List('c'))
res60: List[Char] = List(a, b, c)

Lists - Complexity

  • Insert at front: o(1)
  • Insert at end: o(n)
createList
should return a List create by ListUtil
should add e as first element of l
should add e as last element of l
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants