# Scala第二天

scala> val arr1=new Array[Int](8)

arr1: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0)

scala> val arr2=Array[Int](8)---相当于val arr2=Array(8)该数组只有一个元素8,

arr2: Array[Int] = Array(8)

scala> arr1.length

res4: Int = 8

scala> arr2.length

res5: Int = 1

scala> arr1(0)

res0: Int = 0

scala> arr2(0)

res1: Int = 8

scala> arr2(3)

java.lang.ArrayIndexOutOfBoundsException: 3

... 32 elided 说明arr2只有一个元素

scala> arr1(0)=1

scala> arr1(0)

res3: Int = 1

1、定义变长数组arr4

scala> val arr4=ArrayBuffer[Int](8)

arr4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(8)

scala> arr4.length

res7: Int = 1

2、查看arr4中元素：只有一个元素8

scala> arr4

res9: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(8)

2、在数组的开始插入两个数1,2

scala> arr4.insert(0,1,2)

scala> arr4

res12: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 8)

scala> for(i<-(0 until arr1.length).reverse) println{arr1(i)}

scala> val arr=for(e<-arr1) yield e*3

arr: Array[Int] = Array(3, 0, 0, 0, 0, 0, 0, 0)

scala> arr1.toBuffer

res17: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 0, 0, 0, 0, 0, 0, 0

)

scala> val arr5=arr1.toBuffer

arr5: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 0, 0, 0, 0, 0, 0, 0)

scala> arr5.sorted

res18: scala.collection.mutable.Buffer[Int] = ArrayBuffer(0, 0, 0, 0, 0, 0, 0, 1

)

scala> arr5.sortWith(_>_)

res22: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 0, 0, 0, 0, 0, 0, 0

)

scala> arr5.sum

res19: Int = 1

scala> arr5.max

res20: Int = 1

scala> arr5.min

res21: Int = 0

sortBy算法：按照字典顺序还是数字顺序

scala> arr5.reduce(_+_)：两个数相加结果赋给第一个占位符的数字，再与第二个占位符的数组相加，循环第一步。

res24: Int = 1

map方法：以某种方式改变数组中每个元素的值

scala> arr5.map(_+3)

res25: scala.collection.mutable.Buffer[Int] = ArrayBuffer(4, 3, 3, 3, 3, 3, 3,

)

scala> val scores=Map("tom"->98,"jerry"->100)

scores: scala.collection.immutable.Map[String,Int] = Map(tom -> 98, jerry -> 100)

scala> val scores=Map(("tom",8),("jerry",100))

scores: scala.collection.immutable.Map[String,Int] = Map(tom -> 8, jerry -> 100)

scala> scores("tom")res26: Int = 8

scala> import scala.collection.mutable.map:12: error: object map is not a member of package scala.collection.mutable

import scala.collection.mutable.map

scala> import scala.collection.mutable.Map

import scala.collection.mutable.Map

scala> val scores=Map("tom"->98,"jerry"->100)

scores: scala.collection.mutable.Map[String,Int] = Map(tom -> 98, jerry -> 100)

scala> scores("tom")=101

scala> scores("tom")

res28: Int = 101

scala> val map1=new  scala.collection.mutable.HashMap[String,Int]()

map1: scala.collection.mutable.HashMap[String,Int] = Map()

scala> map1("liudehua")=90

scala> map1

res30: scala.collection.mutable.HashMap[String,Int] = Map(liudehua -> 90)

scala> map1+=("liuruoying",99):15: error: type mismatch;

found  : String("liuruoying")

required: (String, Int)

map1+=("liuruoying",99)

scala> map1+=(("liuruoying",99))

res33: map1.type = Map(liudehua -> 90, liuruoying -> 99)

scala> map1.put("zhagngwuji",100)

res34: Option[Int] = None

scala> map1.put(("zhagngwuji",100)):15: error: not enough arguments for method put: (key: String, value: Int)Option[Int].Unspecified value parameter value.

scala> map1

res37: scala.collection.mutable.HashMap[String,Int] = Map(liudehua -> 90, zhagngwuji -> 100, liuruoying -> 99)

scala> map1.remove(("zhagngwuji",100))

error: type mismatch; found  : (String, Int)

required: String

scala> map1.remove("zhagngwuji")

res40: Option[Int] = Some(100)

scala> map1

res41: scala.collection.mutable.HashMap[String,Int] = Map(liudehua -> 90, liuruo

ying -> 99)

scala> val array=Array((1,"liudehua",20,"F"),(2,"liuruoying"))

array: Array[Product with Serializable] = Array((1,liudehua,20,F), (2,liuruoying

))

scala> array.toMap:15: error: Cannot prove that Product with Serializable <:< (T, U).

array.toMap

scala> val array2=Array((1,"liudehua"),(2,"liuruoying"))

array2: Array[(Int, String)] = Array((1,liudehua), (2,liuruoying))

scala> array2.toMap

res43: scala.collection.immutable.Map[Int,String] = Map(1 -> liudehua, 2 -> liur

uoying)

zip与toMap结合：拉链操作与映射操作

scala> val names=Array("liudehua","liuruoying")

names: Array[String] = Array(liudehua, liuruoying)

scala> val scores=Array(99,98,90)

scores: Array[Int] = Array(99, 98, 90)

scala> names.zip(scores)

res44: Array[(String, Int)] = Array((liudehua,99), (liuruoying,98))

scala> res44.toMap

res45: scala.collection.immutable.Map[String,Int] = Map(liudehua -> 99, liuruoyi

ng -> 98)

scala> val list=List(1,2,3)

list: List[Int] = List(1, 2, 3)

scala> list

res46: List[Int] = List(1, 2, 3)

scala> val list2=List(4,5,6)

list2: List[Int] = List(4, 5, 6)

scala> list++list2

res48: List[Int] = List(1, 2, 3, 4, 5, 6)

import scala.collection.mutable.ListBuffer

scala> val list0=ListBuffer[Int](1,2,3)

list0: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)

list是不可变集合，不可在操作符前

scala> list++=list0:17: error: value ++= is not a member of List[Int]

list++=list0

^

scala> list0++=list

res50: list0.type = ListBuffer(1, 2, 3, 1, 2, 3)

list的一些方法：头、尾（尾巴很长）、求和、reduce、map等等

res51: Int = 1

scala> list.tail

res52: List[Int] = List(2, 3)

scala> list.sum

res53: Int = 6

scala> list.reduce(_+_)

res54: Int = 6

wordcount例子

array.flatMap((x:String)=>x.split(" ")).map(x=>(x,1)).reduceByKey((_+_)).collect

scala> import scala.collection.mutable.HashSet

import scala.collection.mutable.HashSet

scala> val set2=HashSet(1,2,3)

set2: scala.collection.mutable.HashSet[Int] = Set(1, 2, 3)

scala> set2++=HashSet(3,4,5)

res63: set2.type = Set(1, 5, 2, 3, 4)

io.Source读取文件

scala> import scala.io.Source

import scala.io.Source

scala> lazy val file=Source.fromFile("f:/serenity/scala/word.txt")

file: scala.io.BufferedSource =<lazy>

scala> for(line<-file.getLines)println(line)

java.io.FileNotFoundException: f:\serenity\scala\word.txt (系统找不到指定的文件

。)

### 推荐阅读更多精彩内容

• Scala的集合类可以从三个维度进行切分： 可变与不可变集合（Immutable and mutable coll...
时待吾阅读 3,714评论 0 4
• Array //声明变量arr为Array整数类型的数组，包含5个元素。 scala>valarr=newArra...
flyskyzyl阅读 4,845评论 0 4
• PARAMETERIZE ARRAYS WITH TYPES In Scala, you can instanti...
RxCode阅读 144评论 0 0
• 数组是一种可变的、可索引的数据集合。在Scala中用Array[T]的形式来表示Java中的数组形式 T[]。 v...
时待吾阅读 392评论 0 0
• scala文档 包含内容 1基础 2控制结构和函数 3数组相关操作 4映射和元组 5类 6对象 7.包和引入(x）...
zlcook阅读 470评论 0 3