Scala 基础语法(二)

定义变量


val a=1
var b=1
val c="gec"
val d:Int=100
val e:Double=3.14
val f=3.14


 

 

条件表达式


val a=1
val b=if(a<0) -1 else 1
val b=if(a<0){
-1
}else{
1
}

val b=if(a<0)-1

val b=if(a<0) -1 else "gec"

val b=if(a<0) -1 else ()

val b=if(a<0) -1 else if(a==0) 0 else 1

val result={
val b=a+1
val c=b+1
val d=c+1
d
}


 

循环


1 to 10

for(i <- 3 to 10) println(i)

val range=1 to 10

for(i <- range) println(i)

for(i <- 1 to 3){
for(j <- 1 to 3){
println(i+" "+j)
}
}


for(i<- 1 to 3;j<- 1 to 3 if i!=j) println(i+j)

for(i <- 1 to 10) yield i*10

while(a>0){
println(a)
a=a-1
}

1+2
1.+(2)

def m1(x:Int,y:Int):Int=x+y

def m1(x:Int,y:Int)=x+y

m2(4)=4*3*2*1

://error
def m2(x:Int)={
if(x==1) 1
else x*m2(x-1)
}

def m2(x:Int):Int={
if(x==1) 1
else x*m2(x-1)
}


 

声明函数


(x:Int,y:Int) => x+y

val f1=(x:Int,y:Int)=>x+y
f1(1,2)

val f2=(x:Int,y:Int,z:Int)->x+y+z

def m1(x:Int,y:Int):Int=x+y
val f1=(x:Int,y:Int)=>x+y
val f2=m1 _
f2(1,2)

 def m1(x:Int,y:Int):Int=x+y

def m1(x:Int,y:Int)=x+y


 

 

递归函数


def m2(x:Int):Int={
if(x==1) 1
else x*m2(x-1)
}


 

 

匿名函数


(x:Int,y:Int)=>x+y



定义函数


val f1=(x:Int,y:Int)=>x+y

f1(1,2)



定义函数


val f2=(x:Int,y:Int,z:Int)=>x+y+z


 

定义一个方法


def m1(x:Int,y:Int):Int=x+y



定义函数


val f1=(x:Int,y:Int)=>x+y



方法转换函数


val f2=m1 _


 


//定长数组


val array=new Array[Int](5);
val array1=Array(1,2,3,4);


 

 

获取数组元数


array(0)

import scala.collection.mutable.ArrayBuffer
val array=ArrayBuffer[Int]()

array+=1
array+=(1,2,3,4,5,6,7,8,9,10)


array ++=Array(5,6)

//error
val a1=Array(1,2,3)
a1 +=4

array(0)=100
array -=100
array -(2,3,4)
array --Array(5)
array --ArrayBuffer(6,7)

for(i <- array) println(i)


 

 

获取数组的下标


0 until 5

0 until array.length

for(i <- 0 until array.length) println(array(i))
for(i <- (0 until array.length).reverse) println(array(i))
for(i <- array if i%2==0) println(i)
var array3=for(i <- array if i%2==0) yield i*10
array.filter

val f1=(x:Int)=>x%2==0
array.filter(f1)
array.filter((x:Int)=>x%2==0)
array.filter(x=>x%2==0)
array.filter(_ % 2==0)

for(int a:array)
{
if(a%2==0)
{
println
}
}

array.filter(_ % 2==0).map

val f2=(x:Int)=>x*10
array.filter(_ % 2 ==0).map(f2)
array.filter(_ % 2 ==0).map((x:Int)=>x*10)
array.filter(_ % 2 ==0).map(x=>x*10)
array.filter(_ % 2 ==0).map(_*10)

array.max
array.min
array.sum
array.sorted
Array(6,4,5,3,1,2).sorted
Array(6,4,5,3,1,2).sorted.reverse

val map=Map("zhangsan"->30,"lisi"->40)
val map1=Map(("zhangsan",30),("lisi",40))
map("zhangsan")
map1("lisi")
map1.getOrElse("wangwu",50)
map1.getOrElse("lisi",50)
map1.getOrElse("lisi00",50)
import scala.collection.mutable.HashMap

val hmap=HashMap("zhangsan"->30)
hmap +=("list"->40)
hmap +=("lisi"->50,"wangwu"->60)
hmap("lisi")=100
hmap -=("lisi")
hmap.remove("wanggu")


 

 

映射key value

 

//map是不可变的类


scala.collection.immutable.Map
val map=Map("zhangsan"->30)
val map=Map("zhangsan"->30,"lisi"->40)
val map=Map(("zhangsan",30),("lisi",40))
map("zhangsan")
map1("lisi")
map1.getOrElse("wangwu",50)


 

 

//HashMap是可变的类


import scala.collection.mutable.HashMap
var hmap=HashMap("zhangsan"->30)

hmap +=("lisi"->40)
hmap +=("lisi"->50,"wangwu"->60)

hmap -=("lisi")
hmap.remove("wangwu")


 

 

//error


map1+=("a"->50)
map1+=("a"->50)

val tuple=("hadoop",3.14,100)
tuple._1
tuple._2
tuple._3

var array=Array(("zhangsan",30,"football"),("lisi",40,"swimming"))
var array=Array(("zhangsan",30,"Beijing"),("lisi",40,"shanghai"))

array.toMap

val names=Array("zhangsan","lisi")
val ages=Array(30,40,50)
names.zip(ages)
val names=Array("zhangsan","lisi","wangwu")
names.zip(ages)
names.zipAll(ages,"zhaoliu",50)


 

 

list特点:


存储有序的数据


val list=List(1,2,3,4,5,6,7,8,9,10)
list.head

list.tail

List(1).head
List(1).tail


 

//创建一个list集合,里面存储1个数据


1::Nil



//与1::Nil等效写法


1::List()



//创建一个list集合,里面存储3个数据,分别是1,2,3


1::2::3::Nil

val list=List(1,2,3)



在集合头部位置添加数据


list.+:(4)
list.::(5)



在集合尾部位置添加数据


list.:+(4)



在集合尾部位置添加多条数据


list.++(List(4,5,6))



在集合头部位置添加多条数据


list.:::(List(4,5,6))

import scala.collection.mutable.ListBuffer
val list=ListBuffer(1)
list +=2
list +=3

list.append(4,5,6)

list ++=List(7,8)
list ++=ListBuffer(9,10)

list -=1
list -=2
list -=3
list --=List(4,5,6)
list --=ListBuffer(7,8)


 

作业题


创建一个List


val list0=List(1,7,9,8,0,3,5,4,6,2)


 

如何遍历集合


for(i <- list0) yield i*10


 

作用:实现集合的二次赋值


list0.map((x:Int)=>x*10)
//将list0中的每一个元素乘以10后生成一个新的集合
list0.map(x=>x*10)
list0.map(_*10)
list0.map(x=>x+"gec")
//将list0中的每一个元素乘以10后生成一个新的集合
list0.Filter(x=>x%2==0)
list0.Filter(_%2==0)


 


//正向排序


list0.sorted



//反向排序


list0.sorted.reverse


 

 

//二次排序逻辑处理实现


list0.sortBy(x=>x)
list0.sortBy(x=>x).reverse
list0.sortWith((x:Int,y:Int)=>x<y)


 

 

//将List0中的元素4个一组,类型Iterator[List[Int]]


list0.grouped(4)



//将Iterator转换成List


list0.grouped(4).toList



//将多个list压扁成一个List


list0.grouped(4).toList.flatten

val lines=List("hello tom hello jerry","hello jerry","hello kitty")



//先按空格切分,再压平


lines.map(x=>x.split(" "))
lines.map(x=>x.split(" ")).flatten
lines.flatMap(x=>x.split(" "))
lines.flatMap(_.split(" "))


list0.sum
list0.max
list0.min



//并行计算求和


list0.par.sum

def reduce[A1 >: Int](op: (A1, A1) => A1): A1
list0.reduce



//将非特定顺序的二元操作应用到所有元素


list0.reduce((x:Int,y:Int)=>x+y)
list0.reduceLeft(((x:int,y:int)=>x+y)
list0.reduceRight(((x:int,y:int)=>x+y)
list0.reduce(_+_)
list0.reduce(_-_)
list0.reduceLeft(_-_)
list0.reduceRight(_-_)


 

 

//折叠


list0.fold
list0.fold(0)((x:Int,y:Int)=>x+y)
list0.fold(10)((x:Int,y:Int)=>x+y)
list0.foldLeft(10)((x:Int,y:Int)=>x+y)
list0.foldRight(10)((x:Int,y:Int)=>x+y)

res23: List[Int] = List(1, 7, 9, 8, 0, 3, 5, 4, 6, 2)
def fold[A1 >: Int](z: A1)(op: (A1, A1) => A1): A1

list0.fold(10)((x,y)=>{println("x="+x+" y="+y);1;})
list0.fold(10)(_+_)


val list10=List(List(1,2,3),List(4,5,6),List(7,8),List(9,0))



//聚合


list10.par.aggregate
def aggregate[S](z: => S)(seqop: (S, List[Int]) => S,combop: (S, S) => S): S

list10.par.aggregate(10)()

list10.par.aggregate(10)(_+_.sum,_+_)


 

拆分开:


list10.aggregate(0)(
(acc, number) => {
val res1 = acc + number.sum
println("par " + acc + " + " + number+" = "+res1)
res1
},
(par1, par2) => {
val res2 = par1 + par2
println("com " + par1 + " + " + par2+" = "+res2)
res2
}
)

 

println(list10.par.collect{
case _=>Thread.currentThread().getName
}.distinct)

val list=List(1,2,3,4)
val list1=List(1,2,3,4)
val list2=List(3,4,5,6)
list1.union(list2)
list1.intersect(list2)
list1.diff(list2)
list2.diff(list1)

val list10=List(List(1,2,3),List(4,5,6),List(7,8),List(9,0))


list10.aggregate(0)(
(acc, number) => {
val res1 = acc + number
println("par " + acc + " + " + number+" = "+res1)
res1
},
(par1, par2) => {
val res2 = par1 + par2
println("com " + par1 + " + " + par2+" = "+res2)
res2
}
)

 


val set=Set(1,2,3,4)
set.size
set.max
set.min

set & Set(3,4,5,6)

set ++ Set(3,4,5,6)

set --Set(3,4,5,6)

val map=Map("zhangsan"->30,"lisi"->40)
map.keys
map.keySet


 

 

四种遍历方式

 


for(k<-map.keys)println(k+"-> "+map(k))
for(k<-map.keySet)println(k+"-> "+map(k))
for((k,v)<-map) println(k+" > "+v)
map.foreach{case(x,y)=>println(x+"->"+y)}


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-05-25 16:57  Transkai  阅读(281)  评论(0编辑  收藏  举报