Spark+AI案例实战之第1阶段Scala编程第016讲:Scala中List剖析及代码实战

Spark+AI案例实战之第1阶段Scala编程第016讲:Scala中List剖析及代码实战

Spark+AI案例实战之第1阶段Scala编程第016讲:Scala中List剖析及代码实战_第1张图片Spark+AI案例实战之第1阶段Scala编程第016讲:Scala中List剖析及代码实战_第2张图片Spark+AI案例实战之第1阶段Scala编程第016讲:Scala中List剖析及代码实战_第3张图片Spark+AI案例实战之第1阶段Scala编程第016讲:Scala中List剖析及代码实战_第4张图片Spark+AI案例实战之第1阶段Scala编程第016讲:Scala中List剖析及代码实战_第5张图片


package sparkbook2018

object HelloList {
  def main(args: Array[String]): Unit = {

    val immutableList = List(1, 2, 3, 4, 5)
    //  immutableList= 0:: immutableList
    val immutableList1 = 0 :: immutableList
    val immutableList2 = immutableList.:: (6)
    val immutableList3 = immutableList.+:(7)
    val immutableCombinations =(immutableList ++ immutableList1).:::(immutableList2)

    import scala.collection.mutable.ListBuffer
    val mutableList = ListBuffer(1, 2, 3, 4, 5)
    mutableList.+=(0)
    mutableList.foreach(print)

  }
}

:::三个冒号的方法源码:

/** Adds the elements of a given list in front of this list.
   *  @param prefix  The list elements to prepend.
   *  @return a list resulting from the concatenation of the given
   *    list `prefix` and this list.
   *
   *  @usecase def :::(prefix: List[A]): List[A]
   *    @inheritdoc
   *
   *    Example:
   *    {{{List(1, 2) ::: List(3, 4) = List(3, 4).:::(List(1, 2)) = List(1, 2, 3, 4)}}}
   */
  def :::[B >: A](prefix: List[B]): List[B] =
    if (isEmpty) prefix
    else if (prefix.isEmpty) this
    else (new ListBuffer[B] ++= prefix).prependToList(this)

::2个冒号的方法源码:

  // New methods in List

  /** Adds an element at the beginning of this list.
   *  @param x the element to prepend.
   *  @return  a list which contains `x` as first element and
   *           which continues with this list.
   *
   *  @usecase def ::(x: A): List[A]
   *    @inheritdoc
   *
   *    Example:
   *    {{{1 :: List(2, 3) = List(2, 3).::(1) = List(1, 2, 3)}}}
   */
  def ::[B >: A] (x: B): List[B] =
    new scala.collection.immutable.::(x, this)

/** A non empty list characterized by a head and a tail.
 *  @param head the first element of the list
 *  @param tl   the list containing the remaining elements of this list after the first one.
 *  @tparam B   the type of the list elements.
 *  @author  Martin Odersky
 *  @version 1.0, 15/07/2003
 *  @since   2.8
 */
@SerialVersionUID(509929039250432923L) // value computed by serialver for 2.11.2, annotation added in 2.11.4
final case class ::[B](override val head: B, private[scala] var tl: List[B]) extends List[B] {
  override def tail : List[B] = tl
  override def isEmpty: Boolean = false
}







你可能感兴趣的:(AI,&,Big,Data案例实战课程)