/**
知识点
1.scala的变量和常量
变量:var(variable的缩写),变量声明后可以修改
常量:val(value的缩写),常量一经声明,不允许修改
2.scala以换行符为结束标识,不需要加分号;
如果在一行中写多条语句,此时需要用;隔开 比如:var v3="hello"; var v4="world"
3.scala不需要声明对象类型,可以根据结果自动推断对象类型
也可以显示的指定类型,比如:val v4:Int=10
4.scala中没有基本类型,在scala中:
Int,Short,Char,Double,Float,Boolean,Byte,Long 在scala中本质上都是一个类。
所以,var v1=100 这行代码,本质上创建出了Int类的一个对象
即:scala中都是操作对象、方法。所以scala的面向对象要比java更彻底
5.scala中所有类的父类是Any,可类比java的Object
*/
object Demo01 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
var v1=100 //> v1 : Int = 100
v1=200
val v2=100 //> v2 : Int = 100
val v3="hello" //> v3 : String = hello
val v4:Int=10 //> v4 : Int = 10
val v5:String="world" //> v5 : String = world
}
/**
知识点
1.scala中用于对象、类、方法的名称称为表示符。
①标识符区分大小写,比如 val s1=1 val S1=1 是两个不同的标识符
②关键字不能作为标识符
③类名首字母大写
④方法名首字母小写
⑤可以由字母、数字、下划线_ 和$组成
⑥数字不能开头
2.scala中一共有39个关键字:
val var true false null try catch finally throw class object this
super private protected extends with override import package
abstract trait new type lazy sealed if else do while for yield
match case def return implict forSome final
3.scala的运算符包括以下几种类型:
①算术运算符:+ - * / %
②关系运算符:== != > < >= <=
③逻辑运算法:&& || !
④位运算符:& | ! << >>
⑤赋值运算符:= += -= *= /=
补充:scala没有三目(三元)运算符,此外没有++ --操作符
4.scala算符运算符优先级同java。
补充:scala也支持以方法的形式来运算,此时按方法的调用顺序来运算。
比如:val r12=10.+(2).*(3)=36
*/
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次
val r2=s1.*(2) //> r2 : String = hello worldhello world
//取出前n项
val r3=s1.take(5) //> r3 : String = hello
val r31=s1 take 5 //> r31 : String = hello
//取出后n项
val r4=s1.takeRight(2) //> r4 : String = ld
//删除前n项,并返回剩余元素
val r5=s1.drop(2) //> r5 : String = llo world
//删除后n项,并返回剩余元素
val r6=s1.dropRight(2) //> r6 : String = hello wor
val s2="file01.txt" //> s2 : String = file01.txt
//练习1:操作s2,获取文件名,使用两种方法来实现
val r7=s2.take(6) //> r7 : String = file01
val r8=s2.dropRight(4) //> r8 : String = file01
val r9=s2.split("\\.")(0) //> r9 : String = file01
val r10=s2.split("\\.").head //> r10 : String = file01
val r11=10+2*3 //> r11 : Int = 16
val r12=10.+(2).*(3) //> r12 : Int = 36
}
/**
知识点
1.scala的 if else 用法及结构同java。
2.scala的if else 有返回值的,可以接
3.scala的返回值,不需要加return关键字,
通用规则:scala会将方法体{}最后的一行代码当做返回值返回
4.println():打印并换行
print():打印不换行
5.scala的Unit,类比于java的void,即空类型
6.scala的打印函数返回值类型为Unit(空类型)
7.scala通用的化简规则:如果方法体{}中只有一行代码,则{}可以省略
*/
object Demo03 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
val num=20 //> num : Int = 20
val r1=if(num>10){
"big"
1000
}else{
"small"
500
} //> r1 : Int = 1000
val num01=5 //> num01 : Int = 5
val r2=if(num01>5)println("big")else println("small")
//> small
//| r2 : Unit = ()
val r3=if(num01>10)"big" else"small" //> r3 : String = small
val r4=if(num01>5){
println("big")
}
else if(num01==5){
println("equal")
}else{
println("small")
} //> equal
//| r4 : Unit = ()
}
/**
知识点
1.scala的while用法及结构同java
2.scala在通过下标操作集合类型时,使用的是(index),不同于java的[index]
3.scala的for循环,结构:for(i<-某个集合数据){处理代码}
4.scala通用的化简规则:如果调用的方法,参数只有一个,则可以把.()省略
*/
object Demo04 {
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)
var index=0 //> index : Int = 0
while(index<a1.length){
//操作下标操作数组
println(a1(index))
index+=1
} //> 1
//| 2
//| 3
//| 4
for(i<-a1){
println(i) //> 1
//| 2
//| 3
//| 4
}
//练习1:使用scala for,打印出1~10 所有数字
//to方法用于生成指定的区间集合,一般用于for时的遍历范围
1.to(10) //> res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7
//| , 8, 9, 10)
1 to 10 //> res1: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7
//| , 8, 9, 10)
for(i<-1.to(10)){
println(i) //> 1
//| 2
//| 3
//| 4
//| 5
//| 6
//| 7
//| 8
//| 9
//| 10
}
1.to(5) //> res2: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5)
//until也是生成区间集合,含头不含尾
1.until(5) //> res3: scala.collection.immutable.Range = Range(1, 2, 3, 4)
//生产区间集合,并指定步长为2
1.to(5,2) //> res4: scala.collection.immutable.Range.Inclusive = Range(1, 3, 5)
1.until(5,2) //> res5: scala.collection.immutable.Range = Range(1, 3)
//练习2:通过scala的for 打印出如下的图形:
//*
//**
//***
//****
for(i<-1 to 4)println("*".*(i)) //> *
//| **
//| ***
//| ****
//练习3:通过scala的for 打印99乘法表
//每一行中,分隔符统一制表符 \t
//1*1=1
//1*2=2 2*2=4
//1*3=3 2*3=6 3*3=9
//......
for(a<-1 to 9){
for(b<-1 to a){
print(b+"*"+a+"="+b*a+"\t")
}
println
} //> 1*1=1
//| 1*2=2 2*2=4
//| 1*3=3 2*3=6 3*3=9
//| 1*4=4 2*4=8 3*4=12 4*4=16
//| 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
//| 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
//| 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
//| 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
//| 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
//|
for(i<-1 to 10){
if(i>6){
println(i)
} //> 7
//| 8
//| 9
//| 10
}
//scala支持将条件语句写在for循环的条件式中
//这种形式称为for循环的守卫式
for(i<-1 to 10;if i>6&&i%2==0){println(i)} //> 8
//| 10
for(i<-1 to 10;if i>6;if i%2==0){println(i)} //> 8
//| 10
for(a<-1 to 9;b<-1 to a;val sep=if(a==b)"\r\n" else "\t")print(b+"*"+a+"="+b*a+sep)
//> 1*1=1
//| 1*2=2 2*2=4
//| 1*3=3 2*3=6 3*3=9
//| 1*4=4 2*4=8 3*4=12 4*4=16
//| 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
//| 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
//| 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
//| 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
//| 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
val a2=Array(1,2,3,4,5,6) //> a2 : Array[Int] = Array(1, 2, 3, 4, 5, 6)
//通过for遍历a2,获取其中的所有的偶数元素,并返回到一个新的集合中
//for yield表达式:遍历一个集合,返回一个新集合。返回的新集合类型和遍历的集合类型相同
//比如:遍历操作的Array,则返回Array 遍历List,则返回List
val a3=for(i<-a2;if i%2==0)yield{i} //> a3 : Array[Int] = Array(2, 4, 6)
val l1=List(1,2,3,4) //> l1 : List[Int] = List(1, 2, 3, 4)
val l2=for(i<-l1;if i>2)yield{i} //> l2 : List[Int] = List(3, 4)
val m1=Map("tom"->18,"rose"->25,"jim"->30) //> m1 : scala.collection.immutable.Map[String,Int] = Map(tom -> 18, rose -> 2
//| 5, jim -> 30)
for(i<-m1){
println(i) //> (tom,18)
//| (rose,25)
//| (jim,30)
}
for((k,v)<-m1){
println(v) //> 18
//| 25
//| 30
}
m1("tom") //> res6: Int = 18
}
/**
知识点
1.scala 的异常处理机制用法结构同java。不同的在于catch中
需要通过case来对指定的异常进行处理
2.scala 的match,类比于java的switch case
3.scala 的match有返回值,可以接
*/
object Demo05 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
try{
throw new RuntimeException
}catch{
case t:NullPointerException=>{
println("null error")
}
case t:Exception=>{
println("other error")
}
}finally{
println("end")
} //> other error
//| end
val num=10 //> num : Int = 10
val result=num match{
case 10=>{
"111"
}
case 20=>{
"222"
}
} //> result : String = 111
}
import util.control.Breaks._
/**
知识点
1.scala要实现break效果,需要导包。util.control.Breaks._
2.scala通过import关键字导包
3.scala的导包可以出现在代码的任何地方,但是注意包的作用域范围问题。
一般都是放到最上面
4.导包时,_表示导入指定包下的所有类
5.导包时,可以导入指定包下的指定类。比如 import util.control[a.class,b.class]
6.scala没有continue关键字,需要使用break来实现continue效果
7.如果breakable在for外,break是跳出循环的作用
如果breakable在for内,break是continue的作用
*/
object Demo06 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
breakable(
for(i<-1 to 5){
if(i>3){
break
}else{
println(i)
}
}
) //> 1
//| 2
//| 3
for(i<-1 to 5){
breakable(
if(i==3){
break
}else{
println(i)
}
) //> 1
//| 2
//| 4
//| 5
}
}