在Scala中,Array代表的含义和Java中类似,也是长度不可变的数组
此外,由于Scala与Java都运行在JVM中,双方可以互相调用,因此
Scala数组的底层实际上是Java数组。例如字符串数组在底层就是
javaString[],整数数组在底层就是Int[]
数组初始化四种方式:
>>val names = new Array[String](10);
>>val users =Array("nicky","belly","avira","beyonce","kario");
>>val values = Array.fill(5)(10);
>>val nums = Array.ofDim[String](5);
数组赋值:
names(index)= value;
比如
names(0)="hadoop";
names(1)="hbase";
在Scala中,如果需要类似于Java中的ArrayList这种长度可变的集合类,
则可以使用ArrayBuffer。
importscala.collection.mutable._
使用ArrayBuffer()的方式可以创建一个空的ArrayBuffer
valb = ArrayBuffer[Int]();
使用+=操作符,可以添加一个元素,或者多个元素
b+= 1
b+= (2, 3, 4, 5)
代码示例:
scala>val buffer = ArrayBuffer[String]();
buffer:scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer()
scala>buffer+="hadoop";
res50:buffer.type = ArrayBuffer(hadoop)
scala>buffer+=("hive","flume","sqoop","oozie");
res51:buffer.type = ArrayBuffer(hadoop, hive, flume, sqoop, oozie)
scala>:paste
//Entering paste mode (ctrl-D to finish)
for(i <- 0 until buffer.length)
println(buffer(i))
//Exiting paste mode, now interpreting.
hadoop
hive
flume
sqoop
oozie
使用++=操作符,可以添加其他集合中的所有元素
b++= Array(6, 7, 8, 9, 10)
代码示例:
scala>val buffer2 =ArrayBuffer("storm","JStorm","Scala","Spark","Flink");
buffer2:scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(storm, JStorm,Scala, Spark, Flink)
scala>buffer++=buffer2;
res53:buffer.type = ArrayBuffer(hadoop, hive, flume, sqoop, oozie, storm, JStorm,Scala, Spark, Flink)
使用trimEnd()函数,可以从尾部截断指定个数的元素
b.trimEnd(5)
使用insert()函数可以在指定位置插入元素
但是这种操作效率很低,因为需要移动指定位置后的所有元素
b.insert(5,6) //在index=5的位置插入6
b.insert(6,7, 8, 9, 10) //在index=5的位置插入6, 7,8, 9, 10
使用remove()函数可以移除指定位置的元素
b.remove(1)//删除index位置元素
b.remove(index,n) //删除index位置开始之后的
Array与ArrayBuffer可以互相进行转换
b.toArray
a.toBuffer
使用for循环和until遍历Array /ArrayBuffer
使until是RichInt提供的函数
for(i <- 0 until b.length)
println(b(i))
代码示例:
scala>for(i <- 0 until buffer2.length) print(buffer2(i)+" ")
stormJStorm Scala Spark Flink
跳跃遍历Array /ArrayBuffer
for(i<- 0 until (b.length, 2))
println(b(i))
代码示例:
scala>for(i <- 0 until (buffer2.length,2)) print(buffer2(i)+" ")
stormScala Flink
从尾部遍历Array /ArrayBuffer
for(i<- (0 until b.length).reverse)
println(b(i))
代码实例:
scala> for(x <- (0 until buffer.length).reverse)print(buffer(x))
jfedcb
使用“增强for循环”遍历Array /ArrayBuffer
for(e <- b)
println(e)
代码实例:
scala>for(value <- buffer2) print(value +" " )
stormJStorm Scala Spark Flink
vala = Array(1, 2, 3, 4, 5)
valsum = a.sum
15
valmax = a.max
scala>val max = a.max
max:Int = 5
scala.util.Sorting.quickSort(a)
a.mkString
a.mkString(",")
a.mkString("<",",", ">")
a.toString
b.toString
for{子句} yield{变量或表达式}
yield:每一次遍历,记住变量或表达式的值,然后放到一个数组里,
直到遍历完毕,产生了一个新的数组
scala>val a = Array(1,2,3,4,5)
a:Array[Int] = Array(1, 2, 3, 4, 5)
scala>val a2 = for(element <- a) yield { element * element }
a2:Array[Int] = Array(1, 4, 9, 16, 25)
scala>val a4 = for(element <- a if element % 2 == 0) yield element * element
a4:Array[Int] = Array(4, 16)
scala>import scala.collection.mutable._
importscala.collection.mutable._
scala>val b = ArrayBuffer[Int]();
b:scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala>b+=(1,2,3,4,5)
res0:b.type = ArrayBuffer(1, 2, 3, 4, 5)
scala>val b2 = for (ele <- b) yield {ele*ele}
b2:scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 4, 9, 16, 25)
scala>b2.filter(_ % 2 == 0).map(_ * 2)
res1:scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(8, 32)
scala>b2.filter{_ % 2 == 0}.map{_ * 2}
res2:scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(8, 32)
其中_代表占位符,表示当前元素。
>>创建一个不可变的Map
valages = Map("Leo" -> 30, "Jen" -> 25, "Jack"-> 23)
ages("Leo")= 31
>>创建一个可变的Map
scala>val scores =scala.collection.mutable.Map("Nicky"->87,"Nancy"
->88,"Geoger"->75);
scores:scala.collection.mutable.Map[String,Int] = Map(Nicky -> 87, Geoger -> 75,Nancy -> 88)
>>另外一种方式定义Map元素
valages = Map(("Leo", 30), ("Jen", 25), ("Jack",23))
>>创建一个空的HashMap
valages = new scala.collection.mutable.HashMap[String,Int]
获取指定key对应的value,如果key不存在,会报错
valleoAge = ages("Leo")
使用contains函数检查key是否存在
scala>scores.contains("Nicky")
res33:Boolean = true
通过get/getOrElse获取元素
scala>scores.get("Nicky")
res35:Option[Int] = Some(87)
scala>val score = scores.getOrElse("Helen","Bob");
score:Any = Bob
scala>val score = scores.getOrElse("Helen1","Bob");
score:Any = Bob
增加多个元素
scala>ages += ("Mike" -> 35, "Tom" -> 40)
res38:ages.type = Map(Nicky -> 25, Mike -> 35, Tom -> 40)
添加一个元素
scala>ages.put("Leaf",20);
res39:Option[Int] = None
scala> scores("Nicky")= 90
scala>scores.get("Nicky")
res37:Option[Int] = Some(90)6.2 Tuple
scala> vall_users = users+("Ooize"->10,"Sqoop"->23)
scala> ages -="Leaf"
res41:ages.type = Map(Nicky -> 25, Mike -> 35, Tom -> 40)
val l_users_1 =l_users - "Sqoop"
>>遍历map的entrySet
scala>for((key,value) <- ages) print(key +"="+ value+" ")
Nicky=25Mike=35 Tom=40
遍历map的key
for(key<- ages.keySet) print(key)
scala>for(key <- ages.keySet) print(key+" ")
NickyMike Tom
遍历map的value
for(value<- ages.values) print(value+" ")
scala>for(value <- ages.values) print(value+" ")
2535 40
scala>for ((key, value) <- ages) yield (value, key)
res48:scala.collection.mutable.HashMap[Int,String] = Map(35 -> Mike,
40 -> Tom, 25 -> Nicky)
//SortedMap可以自动对Map的key的排序
valages = scala.collection.immutable.SortedMap("leo" -> 30,"alice" -> 15, "jen" -> 25)
//LinkedHashMap可以记住插入entry的顺序
valages = new scala.collection.mutable.LinkedHashMap[String, Int]
ages("leo")= 30
ages("alice")= 15
ages("jen")= 25
其实就是一个keyvalue对组成的一个数据
创建一个tuple
valt = ("Nicky","Chengdu");
遍历tuple
t._1访问第一个元素
t._2访问第二个元素
scala>t._1
res55:String = Nicky
scala>t._2
res56:String = Chengdu
zip操作,也叫拉链操作,把两个数据量相等的集合或者数组进行一个拼接
scala>val names = Array("leo", "jack", "mike")
names:Array[String] = Array(leo, jack, mike)
scala>val ages = Array(30, 24, 26)
ages:Array[Int] = Array(30, 24, 26)
scala>val nameAges = names.zip(ages)
nameAges:Array[(String, Int)] = Array((leo,30), (jack,24), (mike,26))