Appending an element to the end of a list in Scala

后端 未结 5 1421
北恋
北恋 2020-12-02 05:01

I can\'t add an element of type T into a list List[T]. I tried with myList ::= myElement but it seems it creates a strange object and

5条回答
  •  被撕碎了的回忆
    2020-12-02 05:27

    List(1,2,3) :+ 4
    
    Results in List[Int] = List(1, 2, 3, 4)
    

    Note that this operation has a complexity of O(n). If you need this operation frequently, or for long lists, consider using another data type (e.g. a ListBuffer).

提交回复
热议问题