(1)基本语法

变量

val var(可变变量);

数据类型

Byte,Char,Int,Short,Long,String(字符),Float,Double,Boolean(true,flase)。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

列子:

val a = 3;  or  val a:Int = 3*//不可变,不能重新赋值
var b = 4.33; or var b:Double = 4.33 *//可变, 可赋值b = 3.9999;

字面量 :

5 + 6 ; == (5).+(6)//scala是纯面向对象语言,+为方法。3 max 5//(基本数据类型福包装类包含更多运算方法)

自增自减(不能使用a++,a--):

需使用a+=1,a-=1等等

创建Range:

1 to 5 *//Range(1,2,3,4,5) == 1.to(5)
1 until 5*//Range(1,2,3,4)
1 to 10 by 2*//以2为间隔生成Range。
0.5f to 5.9f by 0.3f

输入输出

输入:(import scala.io.StdIn).readInt,readDouble,......
var i = readInt()*//输入整型即可
输出 print,println(输出换行),printf(格式化输出)
println(a), pirntf("output is %d,input is %.2f",a,b)

读写数据

写(import java.io.PrintWriter)

val out = new PrintWrite("path/out.txt");
for( i <- 1 to 5) out.println(i)//写入文件out.txt
out.close

读取(import scala.io.Source)

val readfile = Source.fromFile("out.txt")
val lines = readfile.getLines//返回结果lines为迭代器
for(line<- lines) println(line)

异常处理

例子:

import java.io.FileReader
import java.io.FileNotFoundException
import java.io.IOException
try{
    val f = new FileReader("path/out.txt")
 }
    catch{
    case ex: FileNotFoundException =>
                print("file not found:)
    case ex:IOException =>
                print("io error")
}   finally{
    file.close()
}

控制语句(if,while,for)

val x=3 
if(x>0){ ...} else if(x=3){...} else{...}*//if语句可以赋值给变量 val a = if(x=0) 1 else 0
where(x>0){ x-=1;printf(...)}
do{x+=1 println(i)}where(x<3)

for(i<- 1 to 5) println(i) 
for(i<-1 to 5 if i%2==0) println(i)
fro(i<-1 to 5 if i%2!=0;j<-1 to 10 if j!=i) println(i*j)*//多个生成器+守卫(if)
val r = for(i<- 1 to 5 if i%2==0) yield {println(i);i} *//将for生成器生成的所有值收入集合r( Vector(2,4))

(2) 数据结构(容器collection)

*//import scala.collection,scala.collection.mutable(可变),scala.collection.immutable(不可变)默认

列表(List)

val strList = List("me","you","our")* //不可变列表 方法:head,tail
val otherList = "they"::strList
val List1 = 1::2::3::Nil == List(1,2,3)

集合(Set) 不重复元素

var myset = Set("hadop","spark") *//var可变,库为不可变库
myset+="Scala" *//添加元素
*//导入可变包
import scala.collection.mutable.Set
val mySet = Set("a","b")*//val不可变,但Set可变
mySet+="c"  

映射(map)

val mymap = Map("a"->1,"b"->2)*//键值对,键是唯一的,值不唯一
print(mymap("a"))*//索引
val c = if (mymap.contain("a")) mymap("a") else 0 *//contain判断键是否存在
*//可变map (import scala.collection.mutable.Map)
mymap("a") = 3* //更新
mymap("c")= 4 *//添加新元素
mymap+=("d"->5,"e"->6) *//add
for((key,value)<-mymap) println(key,value)  or  for(key<-mymap.keys)  print(key)   or   mymap.values(生成迭代器)

迭代器(Iteration)

val iter = Iteration(1,2,3,4) 
while(iter.hasNext) { println(iter.next)  } or for(elem<-iter) { printn(iter.next)}

方法:grouped,sliding返回迭代器

val myList = List(1,2,3,4,5)
val a = mylist grouped 3 //按照3分为两组,返回迭代器,a.next=List(1,2,3) a.next = List(4,5)
val b = mylist sliding 3 //3个数滑动 b.next = List(1,2,3) .next = (2,3,4)....

数组(Array)*//(变长数组import scala.collection.mutable.ArrayBuffer)

val myarray = new Array[Int](3)  or val myarray = new Array(2,3,4)
myarray(0) = 1 myarray(1)=2 myarray(2) = 3
for(elem<-0 to 2) { println(myarray(elem)) }

多维数组 :val myarray = Array.ofDim[Int](3,4)
可变数组 :val myarray = ArrayBuffer(10,20,30) myarray+=40 *//方法 insert ,remove

元组(tuple)

//数据类型可以不同
val mytuple = Tuple("a",1,3) println(mytuple._1) println(mytuple._2)
//索引

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄