一、Scala安装
下载网址:Install | The Scala Programming Language
ideal安装
(1)下载安装Scala plugins
(2)统一JDK环境,统一为8
(3)加载Scala
(4)创建工程吧 ^_^
(5)windows+Q,查看是否有Scala
二、Scala常规知识点
(一)Scala初体验
object ScalaTest {def main(args: Array[String]): Unit = {println("hello scala!")}
}
//hello scala!
(二)var 变量、val 常量、Type关键字
// var 变量var name :String="lisi"println("hi " + name) //hi lisiname = "xioaming"println("hi " + name) //hi xioaming
// val 常量val pai = 3.1415926var r =10println("圆的面积:" + pai*r*r) //圆的面积:314.15926
// type关键字:给别名type T=Stringval name:T="java"println( name ) //java
(三)Scala数据类型结构
Any:所有类型的超类(顶级类型)
AnyVal:表示值类型的超类
AnyRef:表示引用类型的超类,对应java.lang.Object
Unit:表示无值,类似于Java中的void
Nothing:所有类型的子类
Null:表示null或空引用
scala与java有着相同的原始数据类型(Byte,Short,Int,Long,Float,Double,Char,Boolend,String)
(四)字符串插值器
(1)S插值器:允许将变量引用、表达式直接插入字面字符中
(2)F插值器
(3)raw插值器
(4)拓展:Scala中值是多少取决于最后一个数据
//(1)s插值器:允许将变量引用、表达式直接插入字面字符中var name = "huahua"println(s"hello $name") //hello huahua//(2)f插值器:允许将变量引用、表达式直接插入字面字符中var pai=3.1415926/* 10个空格 保留小数点两位| | */println(f"派的值为:$pai%10.2f") //派的值为: 3.14//(3)raw插值器println(s"\n b") // b 换行println(raw"\n b") //\n b 不转义//(4)拓展:Scala中值是多少取决于最后一个数据var name = {"唐僧"println("请输出名字:") //请输出名字:"李白一日游"}println(name) //李白一日游
(五)Scala条件控制
(1)if
语法结构:if ( ){ } else if ( ){ } else { }
val age = 18if(age > 18){println("成年人")}else if (age == 18){println("18一枝花")}else{println("受未成年人保护法保护") }//18一枝花
提升版
var age = {println("我是一个代码块,最后一个指令代表返回值")991133}println(s"调用代码块值:$age") //调用代码块值:33var resoultAge=if(age > 33) age -1 else if (age < 18 ) age +1 else ageprintln(resoultAge) //33
(2)while
语法结构:while(){ }
var num = 5while ( num <10 ){println(num)num = num+1}
// 5
// 6
// 7
// 8
// 9
(3) do while
语法结构:do { } whiel ( )
var num = 5do{println(num)num=num+1}while(num<10)
/* 56789*/
(4)for
语法结构( ){ }
var num = 4for( i<-1 to num){println(i)}
/* 1234*/
拓展:步长
var num = 5
// 步长为2for( i<-1 to num by 2){println(i)}
/* 135*/
// 步长为5for( i<-1 to num by 5){println(i)}
/*1*/
(5)小试牛刀
Ⅰ、打印正三角形
var num = 10for(i<-1 to num){for (j<-1 to i){print("*")}println()}*
**
***
****
*****
******
*******
********
*********
**********
Ⅱ、打印等边三角形
var num = 10for(i<-1 to num){for (j<-1 to num-i){print(" ")}for (j<-1 to 2*i-1){print("*")}println()}*********************************************************************************
*******************
Ⅲ、打印倒三角形
var num = 10for(i<-1 to num){for (j<-1 to num-i){print("*")}println()}
*********
********
*******
******
*****
****
***
**
*var num = 10for(i<-1 to num){for (j<-1 to i){print(" ")}for (j<-1 to num-i){print("*")}println()}*********************************************
Ⅳ、打印倒等边三角形
var num = 10for(i<-1 to num){for (j<-1 to i){print(" ")}for (j<-1 to 2*(num-i)-1){print("*")}println()}*********************************************************************************
Ⅴ、三角形抠边
var num = 10for(i<-1 to num){for (j<-1 to num-i){print(" ")}for (j<-1 to 2*i-1){if (i == 1 || i == num || j == 1 || j == 2*i-1){print("*")}else{print(" ")}}println()}** ** ** ** ** ** ** ** *
*******************
(6)拓展:break
import scala.util.control._var num = 10val breakA = new BreaksbreakA.breakable{for (i <-1 to 10 by 2){if (i>5){breakA}println (i)}}/* 13579*/import scala.util.control.Breaks._var num = 20for(i<-1 to num by 2){if (i>5){break()}println(i)}/* Exception in thread "main" scala.util.control.BreakControl135*/
(六)迭代器
// yield:表示当for循环之后将所有值返回给resultvar num = 12for (i <-1 to num ;if i%2==0;if i>5 ;if i%3 == 0){println(i)}
/* 612*/var result = for ( i <-1 to num ; if i%2==0;if i>5 ;if i%3 == 0 ) yield ival it : Iterator[Int]= result.iteratorwhile (it.hasNext){println("迭代器:"+ it.next()) //6,12}
/* 迭代器:6迭代器:12*/
/// 【增强for循环-----不推荐使用】for (re<- result){println("for循环取值:"+ re)
/* for循环取值:6for循环取值:12*/}println(result)println(result(0))println(result(1))
/* Vector(6, 12)612*/
(七)数组
// 创建数组的方式一 var arr = new Array[String](3)arr(0)="华为met 50"arr(1)="华为met 60"arr(2)="华为met 70"for(i<-0 until arr.length){println(arr(i))}
// 创建数组的方式二 调用,伴生对象var arr=Array("华为met50","华为met60","华为met70")for (i<-0 until arr.length){println(arr(i))}
// 创建数组的方式3 区间数组val arr : Array[Int]=Array.range(1,10)val arr : Array[Int]=Array.range(1,10,2)println("length"+arr.length)for(i <-0 to arr.length-1){println(arr(i))}
(八)元组
(1)定义了三个元组,第四个报错 val tp = ( "hello",3.14 , 39 ) println("元组的第一个元素"+tp._1)println("元组的第二个元素"+tp._2)println("元组的第三个元素"+tp._3)println("元组的第四个元素"+tp._4)(2)输出:scalaval tp2=("A",2,'a',4,"scala",6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)println(tp2._5)(3)最多22个,超过22个报错val tp2=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)//Error:(253, 17) too many elements for tuple: 25, allowed: 22// val tp2=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)(4)定义三种类型元组println(new Tuple3[Boolean,Double,Int](true,3.24,100))//(true,3.24,100)(5)遍历元组val tp4=("A",2,'a',4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)【方式一】val iterator : Iterator[Any] = tp4.productIteratorfor (elem<-iterator){println(elem)}【方式二】tp4.productIterator.foreach(x=>println(x))
(九)可变集合
val list1 = scala.collection.immutable.List(1,3,6)list1.foreach(println)
// lamda函数list1.foreach( x =>println(x))val ints = List(1,2,4)println(ints.getClass)val builder: StringBuilder= ints.addString(new StringBuilder("1234"))println("------------------------------")val int2:List[Int]=ints.drop(1)int2.foreach(println(_))println("------------------------------")ints.foreach(println(_))println("------------------------------")
(十)不可变集合
val list = scala.collection.mutable.ListBuffer(1,3,6)println(list(2))