快捷功能
-----------------------------------------------------------------------------------
补全 Tab键
粘贴模式 :paste,退出粘贴模式 ctrl + d
语法
------------------------------------------------------------------------------------
var 定义变量
val 定义常量,相当于java中的final,不能重复赋值
1 to 10 [ 1, 10 ]
1 until 10 [ 1,10 )
字符串求交集
scala> "hello".intersect("he") res31: String = he
BigInt类型
scala> var b = BigInt(999999999) b: scala.math.BigInt = 999999999 scala> b *= b //scala中没有 ++ 或 -- 语法 scala> b res33: scala.math.BigInt = 999999998000000001 scala> b *= b scala> b res35: scala.math.BigInt = 999999996000000005999999996000000001
导包
import scala.math._ //此处 _ 表示所有 sqrt(2) //开方 pow(2,3) //幂函数
apply方法,相当于索引
scala> "hello".apply(2) res40: Char = l scala> "hello"(2) res41: Char = l
scala中表达式都有值
scala> var x = 2 x: Int = 2 scala> var y = if(x > 0) 1 else -1 y: Int = 1 scala> var y = if(x < 0) -1 y: AnyVal = () scala> var p = println(123) 123 p: Unit = () //相当于null
最后一个表达式是整个语句的值
scala> var b = {print() ;234} warning: there was one deprecation warning; re-run with -deprecation for details ()b: Int = 234
输入
scala> var name = readLine("输入名字:") warning: there was one deprecation warning; re-run with -deprecation for details 输入名字:name: String = lasdjfonclnvlolfdgolsdfsadfs scala> name res43: String = lasdjfonclnvlolfdgolsdfsadfs
循环:while、for、for高级用法
break
scala> import scala.util.control.Breaks._ import scala.util.control.Breaks._ scala> for(i <- 1 to 10){ | println(i) | break} 1 scala.util.control.BreakControl scala>
yield
scala> for(i <- 1 to 5)yield i*3 res6: scala.collection.immutable.IndexedSeq[Int] = Vector(3, 6, 9, 12, 15)
函数
object Demo { def main(args: Array[String]): Unit = { //阶乘:非递归和递归 val f1 = fact(5) val f2 = fact2(5) //方法:默认参数,使用:有名参数 val s1 = decorate1("{{","hello","}}") val s2 = decorate2(str="hello") val s3 = decorate3("hello") //变长参数 val v1 = varPar(1,2,3,4,5) val v2 = varPar(0 to(10,2):_*) //将Range对象展开成0,2,4,6,8,10的序列 //过程,即没有 = val g = out(1,2,3) // () } //阶乘 def fact(n:Int)={ var f = 1 for (i <- 1 to n)f *= i f } //阶乘:递归,注意:需要显示定义结果类型 def fact2(n:Int):Int={ if (n == 1) 1 else n*fact2(n-1) } //默认参数 def decorate1(prefix:String,str:String,suffix:String)={ prefix + str + suffix } def decorate2(prefix:String="[",str:String,suffix:String="]")={ prefix + str + suffix } def decorate3(str:String,prefix:String="(",suffix:String=")")={ prefix + str + suffix } //变长参数 def varPar(n:Int*)=n.sum //过程 def out(n:Int*){n.sum} }
lazy,延迟计算,调用时才计算
scala> val x = 1/0 java.lang.ArithmeticException: / by zero ... 32 elided scala> lazy val x = 1/0 x: Int = <lazy> scala> x java.lang.ArithmeticException: / by zero at .x$lzycompute(<console>:14) at .x(<console>:14) ... 32 elided
scala中 _ 线含义
1、import scala.math._ 所有类似 *
2、Range对象展开成数字序列
scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer scala> val arr = ArrayBuffer(1) arr: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1) scala> arr.append(1 to 3:_*) scala> arr res6: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3)
3、取出元组元素,索引从1开始
scala> val t = ("qq","ww") t: (String, String) = (qq,ww) scala> t._1 res9: String = qq scala> t._2 res10: String = ww
4、取出函数本身
scala> val f = scala.math.ceil _
f: Double => Double = <function1>
5、模式匹配中的其他,相当于java中switch-case语句中的default
val ch = 'c' ch match { case 'a' => println(1) case 'b' => println(2) case _ => println(3) }
6、变量默认值
var s:String = _
apply 用法
1、取索引
scala> "12345"(2) res0: Char = 3 scala> "12345".apply(2) res1: Char = 3
2、初始化
scala> val arr = Array.apply[Int](1,2,3) arr: Array[Int] = Array(1, 2, 3) scala> val arr = Array.apply(1,2,3) arr: Array[Int] = Array(1, 2, 3) scala> val arr = Array(1,2,3) arr: Array[Int] = Array(1, 2, 3)
scala中继承层级
AnyVal: root class of all ''value types''
Double、Float、Long、Int、Char、Short、Byte:'numeric value types'Unit、Boolean:'non-numeric value types'
Unit 只有一个值用 () 表示,相当于java中void
Null 类型只有一个实例 null
Nothing 类型没有实例,是任何类型子类型
其他
Option子类型:None 和 Some
Nil 空列表,类型List[Nothing]