Scala数据结构(数组,Map和Tuple)
package com.zy import scala.collection.mutable import scala.collection.mutable.ArrayBuffer object test3 { def main(args: Array[String]): Unit = { //function1() //function2() //function3() //function4() //function5() //function6() //function8() //function9() function10() } //数组 //不变长度数组(真数组) def function1():Unit={ val ints=new Array[Int](20) //赋值 ints(3)=999 for (i<- ints){ println(i) } var arr=Array(1,"中国",9.9) for (s<- arr){ println(s) } //取值 (下标) println(arr(1)) } def function2():Unit={ val arr=Array.fill(3)(math.random) for (s<- arr){ println(s) } } //变长数组 def function3():Unit={ val ab1=new ArrayBuffer[String]() val ab2=ArrayBuffer[Int](100,200) //ab1.insert(4,"中国","日本")//Exception in thread "main" java.lang.IndexOutOfBoundsException: 4 ab1.insert(0,"中国","日本") /*for (v<- ab1){ println(v) }*/ ab1+=("美国","法国")//追加 符号更加简约 for (v<- ab1){ println(v) } ab1++=Array("1","3","6")//追加任何集合 println("----------------------") for (v<- ab1){ println(v) } ab1.insert(1,"菲律宾")//在第一个元素后面 println("----------------------") for (v<- ab1){ println(v) } println("----------------------") for (v<- ab1 if v!="日本"){ println(v) } } def function4():Unit={ val arr=ArrayBuffer[Int](1,4,5,6,0) val res=for (i<- 0 until arr.length) yield arr(i)*2 println(res) // _代表数组中每个元素 //arr.filter(_>0) 判断数组中大于0的元素,返回一个数组 //map(_*2) 每个元素乘于2 arr.filter(_>0).map(_*2).foreach(println(_)) } def function5():Unit={ var arr=Array(6,1,9,0,7) println(arr.max) println(arr.sum) arr.sorted.foreach(println(_))//升序 println("-----------") // _>_ 两个元素比较,大的放前面 降序 arr.sortWith(_>_).foreach(println(_)) println("---------------") arr.sortWith(_>_).foreach(println(_))//升序 println(arr.mkString("And"))//分隔符 println(arr.mkString("<",",",">"))//<6,1,9,0,7> //指定前缀、分隔符、后缀 } //多维数组 def function6():Unit={ var array=new Array[Array[Int]](5) array(0)=Array(1,4,5) array(1)=Array(2,4,5) array(2)=Array(6,4,5) for (a<-array if a!=null;v<-a){//双层循环 println(v) } println("-------------------") var arr2=Array(Array(1,2,3),Array(4,5,6),Array(7,8,9)) for (a<-arr2 ;v<-a){ println(v) } } def function7():Unit={ val arr=Array(4,5,7) //把定长转变长 val buffer = arr.toBuffer buffer.foreach(println(_)) //变长转定长 val ab2=ArrayBuffer[Int](100,200) val array=ab2.toArray } //Map def function8():Unit={ //不可变map(值不能改变) var p=Map("aa"->12,"bb"->15,"cc"->28) // ->是用来创建元组("aa",12) ("bb",15) ("cc",28) //元组:("a",1.99,33,"b") 一组数据【任意类型】 println(p.get("aa"))//Some(12) println(p.get("pppp"))//None //可变map(值可变) val p2=scala.collection.mutable.Map("xx"->11,"yy"->22) //空的可变map val hashMap = new mutable.HashMap[String,Int] //取值 println(p2.get("pp")) println(p2.getOrElse("xx",999))// 如果映射包含键“xx",返回对应的值;否则,返回999 //更新Map中的值 p2("xx")=100 println(p2.get("xx")) //追加 p2+=("g"->1,"h"->2); println(p2) //移除 p2-=("xx") println(p2) //不可变映射 也可以追加 p+=("zz"->999,"mm"->888) println(p) p-=("aa") println(p) //遍历map for ((k,v)<-p){ println("k:"+k+"\t"+"v:"+v) } //遍历键的集合 for (k<- p.keySet) println("k:"+k) //遍历值的集合 for (v<- p.values) println("v:"+v) println("-------------------------") //键 p.foreach(me=>println(me._1)) //值 p.foreach(me=>println(me._2)) //键值对 p.foreach(println(_)) println("--------------------------------") //反转键值对 for ((k,v)<- p) yield println(v,k) var newMap=for ((k,v)<- p) yield(v,k) println(newMap) } def function9():Unit={ // 会按照key的字典顺序进行排序 val mm= scala.collection.immutable.SortedSet("f"->9,"a"->3,"w"->8)//按照字典顺序排序 println(mm) val months = scala.collection.mutable.LinkedHashMap("January" -> 1,"February" -> 2,"March" -> 3) // 创建一个顺序的Map months += ("Fourth" -> 4) println("months=> " + months) // months=> Map(January -> 1, February -> 2, March -> 3, Fourth -> 4) } //元组 def function10():Unit={ val tup=(105,"中国") //空格在拼接多个过程中会引起歧义 println(tup._1+"\t"+tup._2) //元组最大 Tuple22 Tuple3里面有3个元素 val tup2 = new Tuple3[String,Int,Int]("aa",11,44) val (a,b,c)=tup2 println(a) println(b) println(c) //分组 按元素(_)是否是大写区分 println("NewAbcD".partition(_.isUpper))//分组后形成元组 //遍历 tup2.productIterator.foreach(println(_))//最简单方式,但是不让拼接"\t" println() tup2.productIterator.foreach(x=>println(x+"\t")) } }