[置顶] scala笔记
2016-06-25 14:02
218 查看
这篇阅读笔记来自网络。摘自很多自己感觉需要记下来的东西。
Scala中的赋值操作返回的值是Unit
var line = "" while ((line = readLine()) != "") // 在Scala中不能这么用,因为Scala中的赋值操作返回的值是Unit,而""是String类型,不能进行比较,这是函数式编程语言特有的特点 println("Read: "+ line)
<-
- 程序中的<-被称生成器(generator),在执行过程中,集合filesHere中(Array[File])的元素将依次赋给file,file类型为File,打印时调用其toString方法将文件名称打印出来。
scala> for (file <- filesHere)
<-生成器对其它类型的集合也适用,
scala> for(i <- 1 to 5) println("Iteration"+i) Iteration1 Iteration2 Iteration3 Iteration4 Iteration5
在for循环结构中还可以加入if进行过滤操作
val filesHere = (new java.io.File(".")).listFiles for (file <- filesHere if file.getName.endsWith(".scala")) println(file) //还可以加入多个过滤条件,用;隔开 for ( file <- filesHere if file.isFile; if file.getName.endsWith(".scala") ) println(file)
变长数组 ArrayBuffer
//要使用ArrayBuffer,先要引入scala.collection.mutable.ArrayBuffer scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer //创建String类型ArrayBuffer数组缓冲 scala> val strArrayVar=ArrayBuffer[String]() strArrayVar: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer() //+=意思是在尾部添加元素 scala> strArrayVar+="Hello" res63: strArrayVar.type = ArrayBuffer(Hello) //+=后面还可以跟多个元素的集合 //注意操作后的返回值 scala> strArrayVar+=("World","Programmer") res64: strArrayVar.type = ArrayBuffer(Hello, World, Programmer) //显示完整数组内容 scala> strArrayVar res65: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(Hello, World,Programmer) //++=用于向数组中追加内容,++=右侧可以是任何集合 //追加Array数组 scala> strArrayVar++=Array("Wllcome","To","XueTuWuYou") res66: strArrayVar.type = ArrayBuffer(Hello, World, Programmer, Wllcome, To, XueTuWuYou) //追加List scala> strArrayVar++=List("Wellcome","To","XueTuWuYou") res67: strArrayVar.type = ArrayBuffer(Hello, World, Programmer, Wllcome, To, XueTuWuYou, Wellcome, To, XueTuWuYou) //删除末尾n个元素 scala> strArrayVar.trimEnd(3)
List与Array有着诸多的相似之处,但它们有两个明显的区别:
1 List一但创建,其值不能被改变
如前面的nums,改变其值的话,编译器会报错
scala> nums(3)=4 <console>:10: error: value update is not a member of List[Int] nums(3)=4 ^
2 List具有递归结构(Recursive Structure),例如链表结构
List类型和其它类型集合一样,它具有协变性(Covariant),即对于类型S和T,如果S是T的子类型,则List[S]也是List[T]的子类型
scala> var listStr:List[Object]=List("This","Is","Covariant","Example") listStr: List[Object] = List(This, Is, Covariant, Example) //空的List,其类型为Nothing,Nothing在Scala的继承层次中的最低层 //,即Nothing是任何Scala其它类型如String,Object等的子类 scala> var listStr=List() listStr: List[Nothing] = List() scala> var listStr:List[String]=List() listStr: List[String] = List()
Nil表示的是一个空的list.
Mutable and Immutable Collections
Scala collections systematically distinguish between mutable and immutable collections. A mutable collection can be updated or extended in place. This means you can change, add, or remove elements of a collection as a side effect. Immutable collections, by contrast, never change. You have still operations that simulate additions, removals, or updates, but those operations will in each case return a new collection and leave the old collection unchanged.
在scala中,默认使用的都是immutable集合,如果要使用mutable集合,需要在程序中引入
import scala.collection.mutable //由于immutable是默认导入的,因此要使用mutable中的集合的话 //使用如下语句 scala> val mutableSet=mutable.Set(1,2,3) mutableSet: scala.collection.mutable.Set[Int] = Set(1, 2, 3) //不指定的话,创建的是immutable 集合 scala> val mutableSet=Set(1,2,3) mutableSet: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
直接使用Set(1,2,3)创建的是immutable集合,这是因为当你不引入任何包的时候,scala会默认导入以几个包:
这里写图片描述
Predef对象中包含了Set、Map等的定义
这里写图片描述
scala集合类的层次结构:
scala.collection包中的集合类层次结构如下图:
这里写图片描述
These are all high-level abstract classes or traits, which generally have mutable as well as immutable implementations.
scala.collection.immutable包中的类层次结构:
这里写图片描述
scala.collection.mutable包中的类层次结构:
这里写图片描述
可变集合与不可变集合对应关系:
相关文章推荐
- 通过nginx配置文件抵御攻击
- 生成数据库字典
- iOS开发笔记--UIView中的坐标转换
- linux xfs文件系统无法用readdir获取dirent文件类型d_type则用stat获取暨stat函数讲解
- python使用pytesseract识别验证码的方法
- .NET基础拾遗(3)字符串、集合和流3
- AngularJS简单介绍
- Java设计模式——装饰者模式
- Guava Futures异步回调机制源码解析
- linux 经典命令汇集
- Qt 多线程必知必会
- Dreamweaver8.0本地站点的搭建与管理
- 解决centos联网问题
- hdu3293(字符串排序)
- h264中profile和level的含义
- chrome开发者工具(DevTools)中快捷方式列表
- POJ2778&HDU2243&POJ1625(AC自动机+矩阵/DP)
- 利用Canvas+js实现贪吃蛇(4)
- Wmap5 测试80端口 Your port 80 is actually used by :Server: Microsoft-HTTPAPI/2.0
- linux定时重启tomcat