scala基础

scala的学习笔记!

win
------------
cmd下
cmd>scala
在配置了环境变量的情况下,即可进入scala的环境下

scala>:help
查看帮助信息,命令行的使用方法。


scala>:paste
可以把代码大量的粘贴进入里面。



ctrl + D :即可退出scala的环境。

:quit  退出


scala: 本质上是java的脚本语言。scala是面向函数的编程语言。


val:常量
	与java中的final修饰的变量类似,值不可以进行赋值。

var:变量
	可以修改的值。


val = "aa"
	会自动的判断类型。



小技巧:
	ctrl +  K :清空鼠标位置以后的代码。
	ctrl +  A :返回命令行的开头。
	ctrl  +  l : 清屏。
	 按tab键会自动进行补全。

	 
//可以使用res0 + res1 进行计算。
scala> 1
res0: Int = 1
scala> 2
res1: Int = 2
scala> res1 + res0
res2: Int = 3


scala> val b  = 2
b: Int = 2

指定类型:
scala> val c:Int = 3
c: Int = 3

按下tab可以进行方法的补全:
scala> c.to
toByte   toChar   toDouble   toFloat   toInt   toLong   toShort   toString


scala> val a = 1
a: Int = 1

不可变量: 再次赋值报错。
scala> a = 2
<console>:11: error: reassignment to val
       a = 2
         ^

可以赋值
scala> var c = 1
c: Int = 1

scala> c = 2
c: Int = 2



函数 	def(函数的关键词) jia(函数名) (参数列表)={函数核心表达式}
scala> def jia(a:Int,b:Int,c:Int)={
 | a + b + c
 | }

 
jia: (a: Int, b: Int, c: Int)Int

scala> jia(1,2,3)
res8: Int = 6


函数 	def(函数的关键词) jia(函数名) (参数列表):[返回类型]={函数核心表达式}		
scala> def jia1(a:Int,b:Int):Int ={
    | a +  b
    | }	
jia1: (a: Int, b: Int)Int

scala> jia1(1,2)
res9: Int = 3

函数的运用:
递归:循环调用方法本身
scala> def fn(n:Int):Int = {
 | if (n == 1) 1 else n * fn(n - 1)
 | }
fn: (n: Int)Int

scala> fn(2)
res10: Int = 2

scala> fn(3)
res11: Int = 6


递归	
scala> def f(n:Int):Int = {
     | var rs = 1
     | for(i<-(1 to n )){
     | rs *= i
     | }
     | rs
     | }
	f: (n: Int)Int

	scala> f(2)
	res51: Int = 2




使用intersect方法求交集。如下:
scala> "hello".intersect("ho")
res14: String = ho


scala> 1.to(10)
res15: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> 1.until(10)
res16: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)

	
scala没有++或--操作,可以使用+=或-=,这些也都是方法。
scala> var  num = 10
num: Int = 10

scala> ++num
<console>:11: error: not found: value ++
       ++num
       ^

scala> num++
<console>:12: error: value ++ is not a member of Int
       num++
          ^

scala> num--
<console>:12: error: value -- is not a member of Int
       num--
          ^

scala> --num
<console>:11: error: not found: value --
       --num
       ^

scala> num += 1

scala> num
res22: Int = 11

scala> num -=2

scala> num
res24: Int = 9


scala> num
res24: Int = 9


scala> num.-=2
<console>:1: error: ';' expected but integer literal found.
num.-=2
      ^
使用.-方法。在scala中-,+,。。。这些都是方法。
scala> num.-=(2)

scala> num
res26: Int = 7	


Math包
导入Math下的包。
scala> import scala.math._
import scala.math._

开方。
scala> sqrt(4)
res27: Double = 2.0

2的4次方。
scala> pow(2,4)
res28: Double = 16.0

scala> arr(1)
res33: Int = 2

scala> arr(1)
res34: Int = 2

scala> "hello"(0)
res35: Char = h

scala> "hello".apply(1)
res36: Char = e


scala> var arr2 = Array(1,2,3)
arr2: Array[Int] = Array(1, 2, 3)

scala> arr2(1)
res37: Int = 2

			val 数组名称	= Array[数组类型](数值值列表)
scala> val str = Array[String]("h","e","l","l","o")
str: Array[String] = Array(h, e, l, l, o)

scala> str(1)
res38: String = e



使用readLine来获取命令行的输入值。
scala> val name = readLine("输入用户id:");
warning: there was one deprecation warning; re-run with -deprecation for details
输入入你用户I用id:name: String = 12

scala> name
res39: String = 12


while循环:
scala> var n = 1
n: Int = 1

scala> while( n < 10){
     | println(n)
     | n += 1
     | }
1
2
3
4
5
6
7
8
9


for循环: 
scala> for( i <- 1 to 10){
 | println(i)
 | }
1
2
3
4
5
6
7
8
9
10


导入break。 		
scala> import scala.util.control.Breaks.break
import scala.util.control.Breaks.break

遇到break 程序跳出循环:
scala> for( i <- 1 to 10 ){
     | println(i)
     | if(i % 3 == 0){
     | break
     | }
     | }
1
2
3
scala.util.control.BreakControl

使用yield  
scala> for( i <- 1 to 5 )
 | {
 | println(i)
 | }
1
2
3
4
5

生成新的集合: i * 2:
scala> for( i <- 1 to 5 ) yield  i * 2
res44: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10)
 定义修饰串函数
	scala> def decorate(pf:String="<<<", str:String , sf:String=">>>")={
	     | pf + str + sf
	     | }
	decorate: (pf: String, str: String, sf: String)String
	scala> decorate("<<","dss","<<")
	res56: String = <<dss<<
scala> decorate("<<","dss",">>")
res57: String = <<dss>>	

scala> decorate("i","love","you")
res59: String = iloveyou

scala> decorate(pf = "i",str ="hello")
res60: String = ihello>>>

scala> decorate(str="dss",sf="???")
res62: String = <<<dss???


scala中将不带“=”的函数称之为过程,本质上和函数没有区别
scala> def Hi(){
 | println(" hi hello")
 | }
Hi: ()Unit

区分字母大小写:
scala> hi
<console>:15: error: not found: value hi
       hi
       ^
scala> Hi()
 hi hello
可以省略()
scala> Hi
 hi hello


scala> def Hello()={
 | println(" hi ")
 | }
Hello: ()Unit

scala> Hello
 hi

scala> Hello()
 hi


lazy是延迟计算,即表达式不会立即求值,而是使用lazy字样作为占位符,等真正用到的时候再进行计算。
spark的广播变量中使用该中方式进行的实现。

lazy 是不应许var的。只能使用val
scala> lazy var y = x / 0
<console>:1: error: lazy not allowed here. Only vals can be lazy
lazy var y = x / 0
     ^	

不会计算,不会报错。
scala> lazy val y = x / 0
y: Int = <lazy>

当要使用的时候就会计算。
scala> y
java.lang.ArithmeticException: / by zero
  at .y$lzycompute(<console>:15)
  at .y(<console>:15)
  ... 33 elided

一维数组。
scala> val arr = new  Array[Int](3)
arr: Array[Int] = Array(0, 0, 0)

scala> var arr =  Array[Int](3)
arr: Array[Int] = Array(3)

scala> var arr =  Array[Int](1,2,3,4,5)
arr: Array[Int] = Array(1, 2, 3, 4, 5)

修改数组值:
scala> arr(0) = 0

scala> arr
res2: Array[Int] = Array(0, 2, 3, 4, 5)


二维数组:

scala> var arrr = new Array[Array[Int]](3)
arrr: Array[Array[Int]] = Array(null, null, null)

scala> arrr(0)
res12: Array[Int] = Array(1, 2, 3)

scala> arrr
res11: Array[Array[Int]] = Array(Array(1, 2, 3), null, null)

//Int 3,4
scala> val arr = Array.ofDim[Int](3,4)
arr: Array[Array[Int]] = Array(Array(0, 0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0))

scala> arr(0)(1) = 2

scala> arr
res14: Array[Array[Int]] = Array(Array(0, 2, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0))


scala> arr(0) = Array[Int](1,2,3)

scala> arr
res2: Array[Array[Int]] = Array(Array(1, 2, 3), null, null)

scala> arr(1)=Array[Int](1,2,3,4,5,6)

scala> arr
res4: Array[Array[Int]] = Array(Array(1, 2, 3), Array(1, 2, 3, 4, 5, 6), null)

scala> arr(0)(1)
res5: Int = 2

scala> arr(0,1)
<console>:12: error: too many arguments for method apply: (i: Int)Array[Int] in class Array
       arr(0,1)
          ^

scala> arr(1)(1)
res7: Int = 2

scala> arr(1)(2)
res8: Int = 3

scala> arr(1)(7)
java.lang.ArrayIndexOutOfBoundsException: 7
  ... 33 elided


	
数组缓冲区是可变数组,位于scala.collection.mutable包下,使用时需要进行导入。scala使用符号定义的方法有一定规律性:
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val arr = ArrayBuffer[Int](1,2,3)
arr: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)

scala> arr
res15: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)

追加数据
scala> arr+=(4)
res29: arr.type = ArrayBuffer(1, 2, 3, 4)

scala> arr
res30: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4)

scala> arr +=(5)
res31: arr.type = ArrayBuffer(1, 2, 3, 4, 5)

scala> arr
res32: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)



++
两个以上符号命名的方法意味着操纵的是集合元素,如:
arr.++(5 to 8)
+=

带有“=”号的方法意味着对自身的修改,如:

// 改变自身的内容
arr.+=(4)
常用方法

buf.trimEnd(3)                // 删除最后的三个元素
buf.insert(0,1,2,3)           // 在0位置插入1,2,3序列
buf.toArray()             // 转换成数组
buf.reverse()             // 倒序
buf.sum()                 // 求和
buf.max()                 // 最大值
buf.mkString(",")         // 对元素拼接成串
buf.mkString("{",",","}") // 对元素拼接成串


 
scala> arr.+("1")
res47: String = ArrayBuffer(1, 2, 3, 4, 5, 4, 2, 1, 2)1

scala> arr.+("23")
res48: String = ArrayBuffer(1, 2, 3, 4, 5, 4, 2, 1, 2)23

scala> arr.++(3 to 2)
res49: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 4, 2, 1, 2)

scala> arr.++(1 to 2)
res50: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 4, 2, 1, 2, 1, 2)
减掉了集合了。
scala> arr.++(2 to 1)
res51: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 4, 2, 1, 2)


map集合。
scala> val map = Map(1 ->"tom",2 ->"sstr")
map: scala.collection.immutable.Map[Int,String] = Map(1 -> tom, 2 -> sstr) 


scala> val map1 = Map((1,"a"),(2,"b"),(3,"c"))
map1: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c)

scala> println(Map)
scala.collection.immutable.Map$@1fd97a8

scala> print(map1)
Map(1 -> a, 2 -> b, 3 -> c)
	

取固定位置的value
scala> map(2)
res79: String = sstr

使用for循环来 对key-value进行迭代输出,
scala> for((k,v) <- map){
     | println(k + "-->"+v)
     | }
1-->tom
2-->sstr


scala> map(1)
res78: String = tom

scala> map(2)
res79: String = sstr


scala> for(k <- map.key){
     | println(k)
     | }
<console>:13: error: value key is not a member of scala.collection.immutable.Map[Int,String]
       for(k <- map.key){
                    ^
取key
scala> for(k <- map.keys){
     | println(k)
     | }
1
2

取values
scala> for(v <- map.values)
     | {
     | println(v)
     | }
tom
sstr
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值