第80讲:List的泛型分析以及::类和Nil对象学习笔记
接下来几讲回到LIST,进一步深入研究list,对巩固List的知识非常重要。
import scala.collection.AbstractSeq
import scala.collection.LinearSeq
import scala.collection.generic.GenericTraversableTemplate
import scala.collection.LinearSeqOptimized
import scala.collection.generic.GenericCompanion
import scala.collection.immutable._
sealed abstract class List[+A] extends AbstractSeq[A]
//[+A]说明是协变的,
with LinearSeq[A]
with Product
with GenericTraversableTemplate[A,List]
with LinearSeqOptimized[A,List[A]] {
override def companion: GenericCompanion[List] = List
import scala.collection.{Iterable,Traversable,Seq,IndexedSeq}
def isEmpty: Boolean
def head: A
//返回list的第一个元素
def tail: List[A]
//返回list中除第一个元素以外的元素
def ::[B >: A] (x: B): List[B]= new scala.collection.immutable.::(x.this)
}
::类和Nil都是List的子类:
::表示一个非空列表,Nil与之相反,表示空列表。
case object Nil extends List[Nothing]{
override def isEmpty = true
override def head : Nothing = throw new NoSuchElementException("tail of empty list")
override def tail : List[Nothing] = throw new UnsupportedOperationException("tail of empty list")
override def equals(that Any) = that match{
case that1 : scala.collection.GenSeq[_] => that1.isEmpty
case _ => false
}
}
List[Nothing]表示一个空的List,所以Nil的isEmpty方法返回true,head和tail方法抛出异常,
以上内容是从王家林老师DT大数据课程第80讲的学习笔记。
DT大数据微信公众账号:DT_Spark
王家林老师QQ:1740415547
王家林老师微信号:18610086859
scala第80讲视频观看链接:
http://yun.baidu.com/s/1gdnjT3l
我的百度网盘共享的DT大数据梦工厂王家林老师第1-81讲的视频内容:http://pan.baidu.com/s/1qWK9CMo
今日【DT大数据梦工厂视频】《第80讲:scala中List的泛型分析以及::类和Nil对象》
51CTO视频:http://edu.51cto.com/lesson/id-71116.html
优酷视频:http://v.youku.com/v_show/id_XMTMwMzk5NTM0MA==.html?from=y1.7-1.2
腾讯视频:http://v.qq.com/boke/page/s/0/s/s0161b7yg4s.html
56网视频:http://www.56.com/u41/v_MTM4MzA2MTk4.html