第十一(十)天:scala
- 一、scaleDemo
- (一)新建
- (二)Demo01:var与val
- (三)Demo02:和字符串相关的API
- (四)Demo03:if-else语法学习
- (五)Demo04:循环
- (六)Demo05:for yield表达式、map
- (七)Demo06:异常处理
- (八)Demo07:函数1
- (九)Demo08:函数2
- (十)Demo09:函数3(化简)
- (十一)Demo10:函数04(递归)
- (十二)Demo11:函数05(柯里化)
- (十三)Demo12:类
- (十四)Demo13:option类与类型转换
- (十五)Demo14:集合
- (十六)Demo15:List
- (十七)Demo16:Set
- (十八)Demo17:tuple(元组)
- (十九)Demo18:综合练习
- 二、遇到的错误及解决方法
一、scaleDemo
(一)新建
解压发的eclipse,直接打开即可
(1)新建一个scala Project
(2)新建一个scala worksheet
保存后结果就自然在右边以注释的方式出现
(二)Demo01:var与val
package cn.edu.scala
//变量:var,变量赋值是可以修改的
//常量:val,常量一经复制不能修改
//scala不需要显示的制定对象类型,scala可以根据结果自动推断类型
//--var变量可以重复复制,再次复制的数据类型,要和之前的数据类型保持一致
//--scala没有基本数据类型,Int,Short,Char,Double,Float,Boolean,Byte,Long在scala中就是一个类。就是一个对象,scala面向对象比java更纯粹。
//--scala的顶级父类是Any,相当于java的Object
object Demo01 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//--定义一个变量
var v1=100 //> v1 : Int = 100
v1=300
//--定义一个常量
val v2=300 //> v2 : Int = 300
val v3="hello" //> v3 : String = hello
val v4:Int=80 //> v4 : Int = 80
val v5=Array(1,2,3,4) //> v5 : Array[Int] = Array(1, 2, 3, 4)
}
(三)Demo02:和字符串相关的API
package cn.edu.scala
//--和字符串相关的API
object Demo02 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
val s1="hello world" //> s1 : String = hello world
val r1=s1.split(" ") //> r1 : Array[String] = Array(hello, world)
//--重复n次
s1.*(2) //> res0: String = hello worldhello world
//--取出前n个元素
s1.take(3) //> res1: String = hel
//--去除后n个元素
s1.takeRight(4) //> res2: String = orld
//--去掉前n个元素,打印剩下的元素
s1.drop(3) //> res3: String = lo world
//--去掉后n个元素,打印剩下的元素
s1.dropRight(4) //> res4: String = hello w
val s2="A_data_file01.txt" //> s2 : String = A_data_file01.txt
//--要求通过scala语言拿出‘file01’这个文件名 ????
s2.takeRight(10).take(6) //> res5: String = file01
s2.takeRight(10).dropRight(4) //> res6: String = file01
s2.drop(7).take(6) //> res7: String = file01
s2.drop(7).dropRight(4) //> res8: String = file01
s2.split("_").last.dropRight(4) //> res9: String = file01
s2.split("_")(2).dropRight(4) //> res10: String = file01
val s3="100" //> s3 : String = 100
s3.toInt //> res11: Int = 100
s3.toDouble //> res12: Double = 100.0
s3.toFloat //> res13: Float = 100.0
//--定义一个常量
val num=1 //> num : Int = 1
//--从1到10生成一个区间
num.to(10) //> res14: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6,
//| 7, 8, 9, 10)
1.to(10) //> res15: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6,
//| 7, 8, 9, 10)
1.to(10,2) //> res16: scala.collection.immutable.Range.Inclusive = Range(1, 3, 5, 7, 9)
1.until(10) //> res17: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
1.until(10,2) //> res18: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
}
(四)Demo03:if-else语法学习
if else不需要返回值,默认将最后一行作为返回值
package cn.edu.scala
//--if-else语法学习
//--scala中if else作用和java一样,不同的是,scala的if else有返回值,可以接受
//--会将{}最后一行代码当成返回值返回,不需要加return
//--如果方法体返回值类型不一致的时候,则返回类型是Any,建议返回类型要一致
//--如果方法体中只有一行数据,则方法体可以省略
object Demo03 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
val num=4 //> num : Int = 4
val r1=if(num>5){
100
}else {
200
} //> r1 : Int = 200
val r2=if(num >5) "big" else "small" //> r2 : String = small
if(num >5){
println("big")
}else if(num==5){
println("equal")
}else{
println("small")
} //> small
}
(五)Demo04:循环
package cn.edu.scala
//--循环
object Demo04 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
val v1=Array(1,2,3,4,5) //> v1 : Array[Int] = Array(1, 2, 3, 4, 5)
var index=0 //> index : Int = 0
while(index<v1.length){
println(v1(index))
index+=1
} //> 1
//| 2
//| 3
//| 4
//| 5
val a1=Array(1,2,3,4,5,6) //> a1 : Array[Int] = Array(1, 2, 3, 4, 5, 6)
for(i<-a1){
println(i) //> 1
//| 2
//| 3
//| 4
//| 5
//| 6
}
for(i<- 1.to(10)){
println(i) //> 1
//| 2
//| 3
//| 4
//| 5
//| 6
//| 7
//| 8
//| 9
//| 10
}
//--课堂练习,打印99乘法表
for (i<-1.to(9)){
for(j<-1.to(i)){
print(j+"x"+i+"="+i*j+" ")
}
println()
} //> 1x1=1
//| 1x2=2 2x2=4
//| 1x3=3 2x3=6 3x3=9
//| 1x4=4 2x4=8 3x4=12 4x4=16
//| 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
//| 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
//| 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
//| 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
//| 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
//--遍历1到10,打印大于6的偶数
for(i<-1.to(10)){
if(i%2==0&&i>6){
//不知道为何,将下面的语句改为print(i)无结果
println(i)
} //> 8
//| 10
}
}
(六)Demo05:for yield表达式、map
//--for yield表达式
//--map
object Demo05 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
val v1=Array(1,2,3,4,5,6) //> v1 : Array[Int] = Array(1, 2, 3, 4, 5, 6)
//--for yield表达式,可以遍历一个集合,并将操作的数据返回到一个新的集合中
//--在scala中集合是一个大的概念,常见有Array List Set Range Iterator Map
val a1=for(i<-v1)yield(i*2) //> a1 : Array[Int] = Array(2, 4, 6, 8, 10, 12)
val l1=List(1,2,3,4) //> l1 : List[Int] = List(1, 2, 3, 4)
val a2=for(i<-l1)yield(i+10) //> a2 : List[Int] = List(11, 12, 13, 14)
val m1=Map("tom"->18,"jerry"->90,"bob"->78,"rose"->17)
//> m1 : scala.collection.immutable.Map[String,Int] = Map(tom -> 18, jerry -> 9
//| 0, bob -> 78, rose -> 17)
for(i<-m1){
println(i) //> (tom,18)
//| (jerry,90)
//| (bob,78)
//| (rose,17)
}
//--通过二元元组的方式去遍历
for((k,v)<-m1)yield(v+10) //> res0: scala.collection.immutable.Iterable[Int] = List(28, 100, 88, 27)
}
(七)Demo06:异常处理
package cn.edu.scala
//异常处理
//--scala的异常处理机制和java一样,只是形式上有区别,在catch中是通过case来匹配异常进行处理的
//--scala中没有switch case而是match case
//--scala match case 是有返回值的,可以进行接受 如果case匹配到一个结果,则不会向下继续匹配
//--scala中没有continue,在循环判断里面加上breakable即可实现continue的效果
//--scala中导包可以在任意位置
object Demo06 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
try{
// throw new RuntimeException
throw new NullPointerException
}
catch{
// case t:RuntimeException=>{println("runtime")}
case t:RuntimeException=>{println("nullpoint")}
case t:Exception=>{println("other error")}
}finally{
println("end")
} //> nullpoint
//| end
val num = 6 //> num : Int = 6
num match{
case 5=>{
println("555")
}case 6=>{
println("666")
666
}case 7=>{
println("777")
}
} //> 666
//| res0: AnyVal = 666
import util.control.Breaks._
for(i<-1.to(5)){
breakable(
if(i==3){
break
}else{
println(i)
}
) //> 1
//| 2
//| 4
//| 5
}
}
(八)Demo07:函数1
package cn.edu.scala
//--scala定义函数
//--scala定义函数的方式,访问修饰符 def 函数名(形参名:类型,......):返回值类型={方法体}
//--访问修饰符private或者protected,如果没有访问修饰符,则是public
//--scala通用化简规则,方法体最后一行当返回值返回,不需要加return
//--scala会根据返回值自动推断出返回值类型
//--如果方法体前没有= 则说明此方法不需要返回值
object Demo07 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
def f1(a:Int,b:Int):Int={
a+b
} //> f1: (a: Int, b: Int)Int
def f2(a:String,b:Int):String={
a*b
} //> f2: (a: String, b: Int)String
val r1=f1(2,3) //> r1 : Int = 5
val r2=f2("hello",3) //> r2 : String = hellohellohello
def f3(a:String,b:Int)={
a*2
} //> f3: (a: String, b: Int)String
f3("hehe",2) //> res0: String = hehehehe
def f4(a:Int,b:Int){
a+b
} //> f4: (a: Int, b: Int)Unit
f4(1,2)
//--课堂练习1,定义一个函数,提交一个字符串(file.txt),函数最终返回文件名的后缀
def f5(filename:String)={
val r1=filename.split("\\.")
r1(1)
} //> f5: (filename: String)String
f5("file.txt") //> res1: String = txt
//--课堂练习2,定义一个函数,接受一个整数n,打印从0到n的所有数字
def f6(n:Int){
for(i<-0.to(n)){
println(i)
}
} //> f6: (n: Int)Unit
f6(3) //> 0
//| 1
//| 2
//| 3
}
(九)Demo08:函数2
package cn.edu.scala
//--scala函数分为四种
//--1,成员函数,定义在class和object内部的函数,作为类的成员,称之为成员函数
//--2、本地函数,定义在函数中的函数
//--3、匿名函数,最直观的特点就是没有函数名,方法体{}的连接符是=> 作用:可以把匿名函数作为参数进行赋值,也可以把匿名函数当做参数进行传递
//--4、高阶函数,把函数当成参数传递的函数称之为高阶函数
object Demo08 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
def f1(a:Int,b:Int){a+b} //> f1: (a: Int, b: Int)Unit
(a:Int,b:Int)=>{a+b} //> res0: (Int, Int) => Int = <function2>
//--把匿名函数当 成 参数进行赋值
val f2=(a:Int,b:Int)=>{a+b} //> f2 : (Int, Int) => Int = <function2>
f2(2,3) //> res1: Int = 5
//--匿名函数当成参数传递的例子--也称之为高阶函数
def f3(a:Int,b:Int,f:(Int,Int)=>Int)={
f(a,b)
} //> f3: (a: Int, b: Int, f: (Int, Int) => Int)Int
f3(1,3,(a:Int,b:Int)=>{a+b}) //> res2: Int = 4
f3(1,3,(a:Int,b:Int)=>{a*b}) //> res3: Int = 3
f3(1,3,(a:Int,b:Int)=>{a-b}) //> res4: Int = -2
//--课堂练习:定义一个高阶函数F4,达到如下效果
def f4(a:String,b:String,f:(String,String)=>String)={
f(a,b)
} //> f4: (a: String, b: String, f: (String, String) => String)String
f4("hello","word",(a:String,b:String)=>{a+b})
//> res5: String = helloword
//--课堂练习:定义一个高阶函数F4,达到如下效果
def f5(a:Int,b:Int,f:(Int,Int)=>Unit)={
f(a,b)
} //> f5: (a: Int, b: Int, f: (Int, Int) => Unit)Unit
f5(2,5,(a:Int,b:Int)=>{for(i<-a.to(b)){println(i)}})
//> 2
//| 3
//| 4
//| 5
//--课堂练习:定义一个高阶函数F4,达到如下效果
def f6(a:String,f:(String)=>Array[String])={
f(a)
} //> f6: (a: String, f: String => Array[String])Array[String]
f6("hello",(a:String)=>{a.split("e")}) //> res6: Array[String] = Array(h, llo)
}
(十)Demo09:函数3(化简)
package cn.edu.scala
//--scala的化简规则
//--如果方法体中只有一行代码,方法体可以省略
//--如果调用一个scala的方法时,方法参数只有一个的话,则方法的.()可以省略
//--n匿名函数也可以化简
//--如果匿名函数函数参数了行可以推断出,则参数类型可以省略
//--如果匿名函数参数只有一个,则参数列表()可以省略
//--最终的化简规则:可以通过_来代替参数
object Demo09 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
def f1(a:Int,b:Int)=a+b //> f1: (a: Int, b: Int)Int
val s1="hello world" //> s1 : String = hello world
s1.take(2) //> res0: String = he
s1 take 2 //> res1: String = he
1.to(10) //> res2: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7
//| , 8, 9, 10)
1 to 10 //> res3: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7
//| , 8, 9, 10)
1.to(10,2) //> res4: scala.collection.immutable.Range.Inclusive = Range(1, 3, 5, 7, 9)
1 to 10 by 2 //> res5: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
1.until(10) //> res6: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
1 until 10 //> res7: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
1.until(10,2) //> res8: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
1 until 10 by 2 //> res9: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
for(i<-1.to(3)){println(i)} //> 1
//| 2
//| 3
for(i <-1 to 3)println(i) //> 1
//| 2
//| 3
def f2(a:Int,b:Int,f:(Int,Int)=>Int)={
f(a,b)
} //> f2: (a: Int, b: Int, f: (Int, Int) => Int)Int
f2(1,2,(a,b)=>a+b) //> res10: Int = 3
f2(1,2,_+_) //> res11: Int = 3
def f3(a:String,f:(String)=>String)={
f(a)
} //> f3: (a: String, f: String => String)String
f3("hello",(a:String)=>{a*2}) //> res12: String = hellohello
f3("hello",a=>a*2) //> res13: String = hellohello
f3("hello",_*2) //> res14: String = hellohello
}
(十一)Demo10:函数04(递归)
package cn.edu.scala
//--递归函数
object Demo10 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//--给定一个斐波那契数列
//1 1 2 3 5 8 13 21.....
//--编写递归函数f1(n:Int).获取第n项的数字
//--递归函数需要站服务的要素:
//--1、找出结束条件
//--2、找出关系
//--递归函数的返回值需要手动指定
def f1(n:Int):Int= {
if(n==1||n==2){
1;
}else{
f1(n-1)+f1(n-2)
}
} //> f1: (n: Int)Int
f1(5) //> res0: Int = 5
//--课堂练习
//2 3 4 9 16 81.....
//--要求写出递归函数f2(n:Int)判断第n项的数字
def f2(n:Int):Int= {
if(n==1) return 2
if(n==2) return 3
else f2(n-2)*f2(n-2)
} //> f2: (n: Int)Int
f2(6) //> res1: Int = 81
//--课堂练习:给定一个数列
//2 3 4 9 8 27 16 81
//要求写出递归函数f3判断第n项的数字
def f3(n:Int):Int= {
if(n==1) return 2
if(n==2) return 3
if(n%2==0) return f3(n-2)*3
else f3(n-2)*2
} //> f3: (n: Int)Int
f3(8) //> res2: Int = 81
}
(十二)Demo11:函数05(柯里化)
package cn.edu.scala
//--柯里化,可以将接收多个参数的函数,转化为接受单一参数的函数
object Demo11 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
def f1(a:Int,b:Int)={a+b} //> f1: (a: Int, b: Int)Int
//--经过柯里化转换
def f11(a:Int)(b:Int)={a+b} //> f11: (a: Int)(b: Int)Int
f1(2,3) //> res0: Int = 5
f11(2)(3) //> res1: Int = 5
//--写出f2的柯里化表达式
def f2(a:Int,b:Int,c:Int)={a+b+c} //> f2: (a: Int, b: Int, c: Int)Int
def f21(a:Int,b:Int)(c:Int)={a+b+c} //> f21: (a: Int, b: Int)(c: Int)Int
def f22(a:Int)(b:Int,c:Int)={a+b+c} //> f22: (a: Int)(b: Int, c: Int)Int
def f23(a:Int)(b:Int)(c:Int)={a+b+c} //> f23: (a: Int)(b: Int)(c: Int)Int
def f3(a:Int,b:Int,f:(Int,Int)=>Int)={f(a,b)} //> f3: (a: Int, b: Int, f: (Int, Int) => Int)Int
def f31(a:Int)(b:Int,f:(Int,Int)=>Int)={f(a,b)} //> f31: (a: Int)(b: Int, f: (Int, Int) => Int)Int
//--用的比较多,一部分是普通函数,一部分是匿名函数--自建的控制结构-调用层次更加清晰
def f32(a:Int,b:Int)(f:(Int,Int)=>Int)={f(a,b)} //> f32: (a: Int, b: Int)(f: (Int, Int) => Int)Int
def f33(a:Int)(b:Int)(f:(Int,Int)=>Int)={f(a,b)}//> f33: (a: Int)(b: Int)(f: (Int, Int) => Int)Int
f32(2,3)((a:Int,b:Int)=>{a+b}) //> res2: Int = 5
f32(2,3)((a,b)=>a+b) //> res3: Int = 5
f32(2,3)(_+_) //> res4: Int = 5
}
(十三)Demo12:类
package cn.edu.scala
/*
* scala和java一样,通过class来定义一个类
* scala类也有成员变量和成员方法
* 成员变量和成员方法默认就是public修饰的
* 定义成员变量的时候,必须赋予初始值
* 主构造器只有一个,需要在类上声明
* 如果还要定义构造器,只能叫做辅助构造器
* scala的class中不能定义静态成员属性和方法,也没有static关键字
* 如果想要定义静态成员和静态方法,需要定义在object(单例对象)中
* object可以单独存在,也可以和某一个类进行绑定
* 需要将object写到和class一个文件中,并且名字一样
* 通过这种方式将object和class进行绑定,就产生了绑定关系
* 此时object是class的伴生对象,class是object的伴生对象类
* object不用new,直接通过类名,方法名来调用
* scala也是支持重写和重载
* 在重写的时候,如果是一个普通方法,则重写的时候需要加上override关键字
* 如果是一个抽象方法,则不需要加override
* scala也有接口,在scala中称之为特质(trait)
* 类可以混入特质,而且还可以多混入
* 用关键字with来混入
* 特质中也可以定义非抽象方法,目的就是为了多混入区别于抽象类
* 在混入特质的时候,可以使用with来混入,此外如果extends关键字没有被占用,则可以使用extends关键字来混入特质,但是注意,extends关键字只能用一次。必须有extends关键字存在
***/
class Person(n:String,a:Int) {
private var name=n
private var age=a
//--定义辅助构造器
def this(n:String){
this(n,0)
}
def this(a:Int){
this("",a)
}
def this(){
this("",0)
}
def setName(name:String)={
this.name=name;
}
def getName()={
this.name
}
def setAge(age:Int)={
this.age=age
}
def getAge()={
this.age
}
def say()={
println("hello world")
}
}
object Person{
def speak()={
println("say hi")
}
}
package cn.edu.scala
object Demo12 {
println("Welcome to the Scala worksheet")
val p1 = new Person("zhangsan",19)
val p2 = new Person("wangwu")
val p3 = new Person(20)
val p4 = new Person()
// p1.setName("bob")
//p1.setAge(19)
p1.getName()
p1.getAge()
p1.say()
p2.getName()
p2.getAge()
p3.getName()
p3.getAge()
Cooker.food
Cooker.cook()
val item1 = Item("huawei",9999)
val item1=Item("huawei",9999)
val item2=Item
println(item1)
}
package cn.edu.scala
object Cooker {
var food="milk"
def cook()={
println("cook food")
}
}
package cn.edu.scala
class Student extends Person{
override def say()={
println("say hello")
}
}
package cn.edu.scala
abstract class Teacher {
//-定义抽象方法
def makeNote(info:String):String
def teach():Unit
def say()={
}
}
package cn.edu.scala
trait Dance {
def baLei():String
def tiTa(n:Int):Int
}
package cn.edu.scala
//--将来用来封装scala bean对象
//--样例类必须显示的声明柱构造器
//--底层会自动的构造一个空的辅助构造器
//--样例类还会自动混入序列化的特质,省略了extends serializable
//--样例类会自动重写toString方法方便打印
//--样例类也不需要new,可以直接创建对象
case class Item(title:String,Price:Double) {
}
package cn.edu.scala
trait Drive {
def piaoyi():Unit
def shache():String
def start()={
}
}
(十四)Demo13:option类与类型转换
package cn.edu.scala
//--Scala的option类,有两个子类,一个Some一个是None
//--隐式转换
object Demo13 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
def f1(a:Int,b:Int)={
if(b!=0){
Some(a/b)
}else{
None
}
} //> f1: (a: Int, b: Int)Option[Int]
f1(4,2) //> res0: Option[Int] = Some(2)
f1(4,2).getOrElse(1000) //> res1: Int = 2
f1(4,0).getOrElse(1000) //> res2: Int = 1000
val v1 = 100 //> v1 : Int = 100
//--这种方法是显式的转换
val v2:String=v1.toString() //> v2 : String = 100
//--隐式的类型转换
//--定义一个隐式转换函数
//--implicit 关键字定义隐式转换方法,以及隐式转换类或者隐式转换参数
implicit def f2(x:Int)={x.toString} //> f2: (x: Int)String
val v3:String = v1 //> v3 : String = 100
}
(十五)Demo14:集合
package cn.edu.scala
//--集合
object Demo14 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//--创建一个定长的数组,并赋予初始值
val a1=Array(1,2,3,4) //> a1 : Array[Int] = Array(1, 2, 3, 4)
//--创建一个定长数组并指定长度
val a2=new Array[Int](6) //> a2 : Array[Int] = Array(0, 0, 0, 0, 0, 0)
//--定义一个变长的数组
val a3=scala.collection.mutable.ArrayBuffer(1,2,3,4)
//> a3 : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4)
//--给变长数组添加元素
a3.append(5)
a3 //> res0: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
//|
//--获取元素
a1.apply(3) //> res1: Int = 4
val a4=Array(4,7,2,5,9) //> a4 : Array[Int] = Array(4, 7, 2, 5, 9)
a4.max //> res2: Int = 9
a4.min //> res3: Int = 2
a4.sum //> res4: Int = 27
a4.length //> res5: Int = 5
a4.reverse //> res6: Array[Int] = Array(9, 5, 2, 7, 4)
//--取出前三项
a4.take(3) //> res7: Array[Int] = Array(4, 7, 2)
a4.takeRight(2) //> res8: Array[Int] = Array(5, 9)
a4.drop(3) //> res9: Array[Int] = Array(5, 9)
a4.dropRight(4) //> res10: Array[Int] = Array(4)
//--获取头元素
a4.head //> res11: Int = 4
a4.last //> res12: Int = 9
//--课堂练习 计算出a5后4项的数据的均值
val a5=Array(2,5,1,7,8,10) //> a5 : Array[Int] = Array(2, 5, 1, 7, 8, 10)
val r1=a5.takeRight(4).sum/4 //> r1 : Int = 6
//--练习2.计算出a5中间项(5,1,7,8)中的极差(最大值-最小值)
a5.drop(1).dropRight(1).max-a5.drop(1).dropRight(1).min
//> res13: Int = 7
val a6 = Array(1,2,3) //> a6 : Array[Int] = Array(1, 2, 3)
val a7 = Array(3,4,5) //> a7 : Array[Int] = Array(3, 4, 5)
//--求交集
val r4 = a6.intersect(a7) //> r4 : Array[Int] = Array(3)
//--求并集
val r5 = a6.union(a7) //> r5 : Array[Int] = Array(1, 2, 3, 3, 4, 5)
//--去重
r5.distinct //> res14: Array[Int] = Array(1, 2, 3, 4, 5)
//--取差集
val r6 = a6.diff(a7) //> r6 : Array[Int] = Array(1, 2)
val r7 = a7.diff(a6) //> r7 : Array[Int] = Array(4, 5)
//把数组中的字符转化为字符串,并指定分隔符
a6.mkString("|") //> res15: String = 1|2|3
//遍历
for(i<-a5)println(i) //> 2
//| 5
//| 1
//| 7
//| 8
//| 10
a5.foreach{(x:Int)=>println(x)} //> 2
//| 5
//| 1
//| 7
//| 8
//| 10
a5.foreach (println(_)) //> 2
//| 5
//| 1
//| 7
//| 8
//| 10
}
(十六)Demo15:List
package cn.edu.scala
//--List
/*
1、max、min
2、sum
3、take、takeRight
4、drop、dropRight
5、head、last
6、mkString
7、intersect、union、diff
8、distinct
9、filter
10、exists
11、map
12、reduce
13、sortBy
14、reverse
15、foreach
16、groupBy
17、mapValues(Map)
18、flatMap
重点掌握:map filter sortBy reduce groupBy mapValues flatmap
**/
object Demo15 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//--创建定长的List
val l1=List(1,2,3,4) //> l1 : List[Int] = List(1, 2, 3, 4)
//--创建一个变长的List
val l2 = scala.collection.mutable.ListBuffer(1,2,3,4)
//> l2 : scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)
l1.apply(0) //> res0: Int = 1
l1(0) //> res1: Int = 1
l2.append(6)
l2 //> res2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 6)
val l3=List(1,2,3,4,5,6,7,8) //> l3 : List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)
val r1=l3.filter{_>4} //> r1 : List[Int] = List(5, 6, 7, 8)
//--课堂练习,通过filter过滤出大于2的偶数
val r2=l3.filter{_>2}.filter{_%2==0} //> r2 : List[Int] = List(4, 6, 8)
val r3=l3.filter{x=>x>2&&x%2==0} //> r3 : List[Int] = List(4, 6, 8)
//课堂练习,计算l3中所有奇数的和
val r4=l3.filter{_%2!=0}.sum //> r4 : Int = 16
val l4=List("tom M 23","rose F 18","jerry M 30","jim F 35")
//> l4 : List[String] = List(tom M 23, rose F 18, jerry M 30, jim F 35)
//--课堂练习,过滤出所有男性的数据
val r5=l4.filter(_.split(" ")(1).equals("M")) //> r5 : List[String] = List(tom M 23, jerry M 30)
val r6=l4.filter(_.contains("M")) //> r6 : List[String] = List(tom M 23, jerry M 30)
//--课堂练习,求出l4中年龄大于20岁的数据
val r7=l4.filter(_.split(" ")(2).toInt>20) //> r7 : List[String] = List(tom M 23, jerry M 30, jim F 35)
//--exists方法根据指定的匿名函数判断元素是否存在
//--如果元素存在则返回true,不存在则返回false
val l5=List(1,2,3,4,5) //> l5 : List[Int] = List(1, 2, 3, 4, 5)
val r8=l5.exists{_>4} //> r8 : Boolean = true
//--map方法,映射方法。作用:根据指定的匿名函数规则将集合中的元素从一个形式映射为另外一个形式
//--并将映射后的元素返回到一个新的集合中,元素的个数是不变的
val r9=l5.map{x=>x*2} //> r9 : List[Int] = List(2, 4, 6, 8, 10)
val r10 = l5.map(x=>x.toString) //> r10 : List[String] = List(1, 2, 3, 4, 5)
val l6 = List("hello world","hello scala") //> l6 : List[String] = List(hello world, hello scala)
val r11 = l6.map{x=>x.split(" ")} //> r11 : List[Array[String]] = List(Array(hello, world), Array(hello, scala))
//|
val l7=List("tom M 23","rose F 18","jim M 30") //> l7 : List[String] = List(tom M 23, rose F 18, jim M 30)
//--课堂练习,操作l7做映射,返回新的集合List("tom","rose","jim")
val r12=l7.map{x=>x.split(" ")(0)} //> r12 : List[String] = List(tom, rose, jim)
//--课堂练习,求出l7中所有人的年龄之和
val r13=l7.map{x=>x.split(" ")(2).toInt}.sum //> r13 : Int = 71
//--课堂练习,计算出l7中男性的平均年龄
val r14=l7.filter(x=>x.contains("M")).map{x=>x.split(" ")(2).toInt}
//> r14 : List[Int] = List(23, 30)
val r15=r14.sum/r14.length //> r15 : Int = 26
//--reduce规约方法
val l8 = List(1,2,3,4) //> l8 : List[Int] = List(1, 2, 3, 4)
//--底层逻辑
//--第一次:x=1,y=2,x+y=3
//--第二次:x=3,y=3,x+y=6
//--第三次:x=6,y=4,x+y=10
val r16=l8.reduce{(x,y)=>x+y} //> r16 : Int = 10
val r17=l8.reduce{(x,y)=>x*y} //> r17 : Int = 24
//--sortBy方法根据指定的匿名函数规则进行排序,实现升序或降序
//--将排序后的结果返回到一个新的集合中
//--可以通过reverse达到降序的效果
val l9=List(1,3,2,5,4) //> l9 : List[Int] = List(1, 3, 2, 5, 4)
val r18=l9.sortBy{x=>x} //> r18 : List[Int] = List(1, 2, 3, 4, 5)
r18.reverse //> res3: List[Int] = List(5, 4, 3, 2, 1)
val l10=List("b 3","a 4","c 2","d 1") //> l10 : List[String] = List(b 3, a 4, c 2, d 1)
//--课堂练习。操作l10,安装数字进行降序排序
val r19=l10.sortBy(x=>x.split(" ")(1).toInt).reverse
//> r19 : List[String] = List(a 4, b 3, c 2, d 1)
val r20=l10.sortBy(x=> - x.split(" ")(1).toInt) //> r20 : List[String] = List(a 4, b 3, c 2, d 1)
val l11=List("tom M 23","rose F 18","jerry M 30","jim F 35","bob M 55","xiaoming M 43")
//> l11 : List[String] = List(tom M 23, rose F 18, jerry M 30, jim F 35, bob M
//| 55, xiaoming M 43)
//--课堂练习:计算男性年龄最高的前两名的年龄和
val r21=l11.filter(x=> x.split(" ")(1).equals("M")).sortBy(x=> -x.split(" ")(2).toInt)
//> r21 : List[String] = List(bob M 55, xiaoming M 43, jerry M 30, tom M 23)
r21.map(x=>x.split(" ")(2).toInt).take(2).sum //> res4: Int = 98
l11.foreach(println(_)) //> tom M 23
//| rose F 18
//| jerry M 30
//| jim F 35
//| bob M 55
//| xiaoming M 43
l11.toArray //> res5: Array[String] = Array(tom M 23, rose F 18, jerry M 30, jim F 35, bob
//| M 55, xiaoming M 43)
val l12=List(1,2,3) //> l12 : List[Int] = List(1, 2, 3)
val l14=l12.::(4) //> l14 : List[Int] = List(4, 1, 2, 3)
}
(十七)Demo16:Set
package cn.edu.scala
//Set
object Demo16 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//--创建定长set,会自动去重
val s1=Set(1,2,3,3,4,5) //> s1 : scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)
//--创建变长的set
val s2=scala.collection.mutable.Set(1,2,3,4)
//> s2 : scala.collection.mutable.Set[Int] = Set(1, 2, 3, 4)
//--创建定长Map
val m1=Map("tom"->18,"rose"->20) //> m1 : scala.collection.immutable.Map[String,Int] = Map(tom -> 18, rose -> 20
//| )
//--创建变长map
val m2=scala.collection.mutable.Map("zhangsan"->45,"lisi"->66)
//> m2 : scala.collection.mutable.Map[String,Int] = Map(lisi -> 66, zhangsan ->
//| 45)
//--给变长的map添加元素
m2+=("xiaowang"->55) //> res0: cn.edu.scala.Demo16.m2.type = Map(lisi -> 66, xiaowang -> 55, zhangsan
//| -> 45)
//--此方式如果拿不存在的键值时,会抛出异常
//m2.apply("zhangsan")
//m2("wangmazi")
m2.get("zhangsan").getOrElse(100) //> res1: Int = 45
m2.get("wangmazi").getOrElse(99) //> res2: Int = 99
m2.keys.toList //> res3: List[String] = List(lisi, xiaowang, zhangsan)
m2.values.toList //> res4: List[Int] = List(66, 55, 45)
val m3=Map("tom"->18,"rose"->20,"jim"->33)//> m3 : scala.collection.immutable.Map[String,Int] = Map(tom -> 18, rose -> 20
//| , jim -> 33)
val r1=m3.filter{case (k,v)=>v>=20} //> r1 : scala.collection.immutable.Map[String,Int] = Map(rose -> 20, jim -> 33
//| )
val r2=m3.mapValues{x=>x+10} //> r2 : scala.collection.immutable.Map[String,Int] = Map(tom -> 28, rose -> 30
//| , jim -> 43)
}
(十八)Demo17:tuple(元组)
package cn.edu.scala
//--tuple表示元组
object Demo17 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//--创建一个元组
val t1=(1,2,"hello",3.5) //> t1 : (Int, Int, String, Double) = (1,2,hello,3.5)
//--元组是通过位置下标进行操作,下标是从1开始的
t1._3 //> res0: String = hello
val t2=(1,2,(3,4)) //> t2 : (Int, Int, (Int, Int)) = (1,2,(3,4))
t2._3._2 //> res1: Int = 4
val t3=(1,(2,3),(4,Array(5,6))) //> t3 : (Int, (Int, Int), (Int, Array[Int])) = (1,(2,3),(4,Array(5, 6)))
t3._3._2(0) //> res2: Int = 5
val m1 = Map("tom"->14,"jim"->66) //> m1 : scala.collection.immutable.Map[String,Int] = Map(tom -> 14, jim -> 66)
//|
m1.filter{case (x,y)=>y>20} //> res3: scala.collection.immutable.Map[String,Int] = Map(jim -> 66)
m1.filter{x=>x._2>20} //> res4: scala.collection.immutable.Map[String,Int] = Map(jim -> 66)
m1.map{case (x,y)=>(x,y+10)} //> res5: scala.collection.immutable.Map[String,Int] = Map(tom -> 24, jim -> 76)
//|
m1.map{x=>(x._1,x._2+10)} //> res6: scala.collection.immutable.Map[String,Int] = Map(tom -> 24, jim -> 76)
//|
}
(十九)Demo18:综合练习
package cn.edu.scala
//--综合练习
object Demo18 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
val l1=List(("bj",12000),("sh",8000),("bj",9200),("sh",13000))
//> l1 : List[(String, Int)] = List((bj,12000), (sh,8000), (bj,9200), (sh,13000
//| ))
//--课堂练习1,过滤出北京地区的数据
val r1 = l1.filter{x=>x._1.equals("bj")} //> r1 : List[(String, Int)] = List((bj,12000), (bj,9200))
//--课堂练习2,计算出上海地区的平均薪资
val r2 = l1.filter{x=>x._1.equals("sh")}.map(x=>x._2)
//> r2 : List[Int] = List(8000, 13000)
val r3 = r2.sum/r2.length //> r3 : Int = 10500
val r4 = l1.groupBy(x=>x._1) //> r4 : scala.collection.immutable.Map[String,List[(String, Int)]] = Map(bj ->
//| List((bj,12000), (bj,9200)), sh -> List((sh,8000), (sh,13000)))
val l6 = List("hello world","hello hdfs") //> l6 : List[String] = List(hello world, hello hdfs)
//--flatmap扁平化处理和map不一样的地方在于,flatmap可以改变处理后数据的个数
//--读取一行数据,处理一行数据,拿到每行数据的内容
val r5=l6.flatMap{x=>x.split(" ")} //> r5 : List[String] = List(hello, world, hello, hdfs)
//--统计单词数量
val l7=List("hello world","hello hdfs","hello hadoop","hello flume","hadoop flume","hello hbase")
//> l7 : List[String] = List(hello world, hello hdfs, hello hadoop, hello flume
//| , hadoop flume, hello hbase)
val r6=l7.flatMap{x=>x.split(" ")}.groupBy(x=>x).mapValues(v=>v.length)
//> r6 : scala.collection.immutable.Map[String,Int] = Map(world -> 1, hadoop ->
//| 2, flume -> 2, hello -> 5, hbase -> 1, hdfs -> 1)
val r7=l7.flatMap{x=>x.split(" ")}.groupBy(x=>x).map{case(k,v)=>(k,v.length)}
//> r7 : scala.collection.immutable.Map[String,Int] = Map(world -> 1, hadoop ->
//| 2, flume -> 2, hello -> 5, hbase -> 1, hdfs -> 1)
}
二、遇到的错误及解决方法
(一)打开eclipse打不开。弹出弹窗报错an error…log
解决方法:
修改环境变量,将path中java1.8移到最前面如下图
修改好后在命令行输入java -version检验