?
0. 说明
?
?
?
?
1. 基本概念
1.0 Scala是什么
Scala?是一门多范式的编程语言,设计初衷是要集成面向对象编程和函数式编程的各种特性。
?
1.1 变量 & 常量
在 Scala 中,使用关键词 "var" 声明变量,使用关键词 "val" 声明常量。
# 变量,可以对值进行更改
var a = 10
# 常量,不能更改
val a = 10
?
1.2 函数 & 类的区别
- 函数,不需要使用类,直接调用
- 方法需要通过类调用
?
1.3 产生1 ~ 10的序列
1 to 10 相当于?1.to(10)?

?
1.4?字符串交集
"hello".intersect("world")

?
1.5?BigInt 类型
var b = BigInt(999999)
?
1.6 Scala 自增
Scala 不像 Java 那样使用 a++? a-- 作为自增自减
而是使用以下
a += 1
a -= 1
?
1.7 使用函数
导入 math 下的所有成员
import scala.math._
sqrt(2) //开方
pow(2,3) //幂函数

?
1.8 apply 方法
"hello".apply(0)?
"hello"(0)?
?

?
1.9?表达式
val x = 2
val b = if(x > 0 ) 1 else -1 // Scala中不包含三元运算

?
// b:AnyVal = ()
val b = if(x < 0) -1
val x:Unit = () //相当于null

?
1.10 语句整体
val b = if(x > 0 ){1
}else -1
?
1.11 粘贴
:paste
ctrl + d //退出粘贴模式
?
1.12 语句终止
同一行多条语句之间用 " ; " 分隔
val b = {println("a") ;println("bbb");100}

?
1.13 输出 & 输入
// 输出
println("xxx")
printf("aaa %s bb %s","abc","def")

?
// 输入,读取控制台输入
var name = readLine("请输入名字:")
println(name)

?
?
?
2. 循环
2.1 while 循环
var n = 0 ;
while(n < 10){
println(n)
n +=1
}

?
2.2 for 循环
for(i <- 1 to 10){
println(i)
}

?
2.3 使用 while 和 for 输出 99 表格
?
var r = 1
while(r < 10){
var c = 1
while(c <= r){
printf("%dx%d=%dt",c,r,(c * r))
if(c == r) println()
c+=1
}
r +=1
}

?
?
?
//for循环
for(i <- 1 to 9){
for(j <- 1 to i){
printf("%dx%d=%dt",j,i,(i * j))
if(i == j)println()
}
}
?

?
?
//until,1 ~ 9
val a = 1 until 10
//break和continue
import scala.util.control.Breaks._
for(i < 1 to 10){
println(i)
break ;
}
//高级for循环,迪尔卡积
for(i <- 1 to 9 ; j <- 1 to i){
printf("%dx%d=%dt",i i * j)
if(i == i)println()
}
//守卫条件for循环
for(i <- 1 to 9 ; j <- 1 to i if( i == 2 * j)){
printf("i=%d,j=%drn",j)
}
//yield,产生新集合
for(i <- 1 to 5) yield i * 2
//定义函数
def add(a:Int,b:Int):Int = a + b
//定义函数,实现n的阶乘
5! = 5 x 4x 3x 2x 1 ;
def fac(n:Int) = {
var r = 1
for(i <- 1 to n){
r *= i
}
r
}
//递归函数必须显式定义返回类型
def fac(n:Int):Int = if(n == 1) 1 else n * fac(n - 1)
//定义函数,参数数带有默认值
def decorate(prefix:String="{{{",str:String,suffix:String="}}}") = {
prefix + str + suffix
}
//带名参数
decorate(str = "hello" )
//变长参数
def add(x:Int*) = {var sum = 0 ;for(i <- x)sum += i ;sum}
add(1,2,3,4,5)
add(1 to 5:_*) //把range对象打散成数字序列,1,5
//过程,没有=的函数,
def out(){println("hello world")}
//lazy,延迟计算
val x = 1/ 0
lazy val x = 1 / 0
x
//定义数组 [泛型]
val arr = new Array[Int](10)
arr(0) = 1
arr(1) = 2
arr(2) = 3
for(i <- 0 until arr.length ) arr(i) = i + 1
val arr = Array[String]("hello","world")
val arr = Array("hello","world")
val arr = Array.apply("hello","world")
val arr = Array.apply[String]("hello","world")
val arr = Array[Int](100) //一个100的值
val arr = new Array[Int](100) //100个0
//命名法则:
//++ 加多个元素
//+ 加一个元素
//+= =表示修改自身的内容
//数组缓冲区(集合)
//=修改自身的内容,否则生成新集合
import scala.collection.mutable.ArrayBuffer
val buf = ArrayBuffer[Int](1,3)
val x = buf.++(4 to 10)
buf.++=(4 to 10)
buf.+=(1,4) //追加多个元素
buf += 12
buf.trimEnd(3) //移除末尾3个元素
buf.insert(0,-2,-1,0) //在指定的索引位置,插入数字序列
buf.toArray()
//倒序遍历
for(i <- (0 until buf.length).reverse) println(buf(i))
//常用操作
buf.sum
//mkStrig,连接元素成串
buf.mkString(",")
buf.mkString("{{{",","}}}")
//多维数组
val arr = new Array[Array[Int]](2)
val arr = Array.ofDim[Int](3,4)
//java集合和scala buffer之间的互操作
val list:java.util.List[String] = new java.util.ArrayList[String]()
list.add("hello")
list.add("world")
//定义函数,使用java的list类型
def mylist(list:java.util.List[String]) = println(list.size())
val buf = ArrayBuffer[String]("how","are","you")
mylist(buf) //类型不匹配
import scala.collection.JavaConversions.bufferAsJavaList
mylist(buf) //ok
//定义scala参数缓冲区的函数
def mybuf(buf:scala.collection.mutable.Buffer[String]) = println(buf.size())
import scala.collection.JavaConversions.asScalaBuffer //隐式转换
mybuf(list);
//映射map,不可变
val map = Map(1->"tom",2->"tomas",3->"tomasLee")
map(1)
map(2)
map(3)
//可变集合,导类指定别名
import scala.collection.mutable.{Map=>MMap}
val map = MMap(1->"tomas",2->"tomasLee")
map(1)
//对偶是一个kv对,等价于java Map中Entry.
val c = 100->"tomas"
val c = (100,"tomas")
//map
val map = MMap(("001","tom"),("002","tomas"),("003","tomasLee"))
//迭代map
for(k <- map.keys) println(k)
for(v <- map.values) println(v)
for((k,v) <- map) println(k + " : " + v)
for(c <- map) println(c._1 + " : " + c._2)
//
map.contains("001")
val v = map.getOrElse("001","nobody")
map.+=(( "006","tom6"))
map.+=("006"->"tom6")
map.-=("006")
val m1 = scala.collection.immutable.SortedMap(1->"tom1",3->"tom3",2->"tom2")
for(c <- m1) println(c)
//元组tuple
val t = ( 1,"tom",12)
val t:Tuple3[Int,String,Int] = new Tuple3[Int,Int](1,12) ;
t._1
t._1 = 100 //错的,元组的组员是不能重新赋值
//拉链 zip
val ids = (1,3)
val names= ("tom1","tom2","tom3")
ids.zip(names)
ids.zipAll(names,"nobody") //全拉链,-1左边填充元素,"nobody"右侧填充元素
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
