本文作者:林伟兵,叩丁狼高级讲师。原创文章,转载请注明出处。
1. 定长数组
数组的创建:
//通过指定数组长度来创建数组,系统会为其赋上默认的值
scala> new ArrayString
res0: Array[String] = Array(null, null, null, null, null)
scala> new ArrayDouble
res1: Array[Double] = Array(0.0, 0.0, 0.0, 0.0, 0.0)
//如果想在创建的时候指定元素的内容,可以调用数组伴生对象的的apply方法
scala> Array("Hadoop","Storm","Spark")
res2: Array[String] = Array(Hadoop, Storm, Spark)
数组的操作包含了对元素的读写,数组的长度 求最大值最小值等,除此之外还可以打印数组的长度:
//存值取值的操作
scala> res0(2)
res3: String = null
scala> res0(2) = "Hello world"
scala> res0
res5: Array[String] = Array(null, null, Hello world, null, null)
scala> Array(1,2,3,4,5,6,0)
res6: Array[Int] = Array(1, 2, 3, 4, 5, 6, 0)
//求极限值 求和
scala> res6.min
res7: Int = 0
scala> res6.max
res8: Int = 6
scala> res6.sum
res9: Int = 21
如果想打印一个数组内容,默认的toString是无法实现的,可以使用mkString来实现打印, 如下:
scala> res6.toString
res14: String = [I@1f6ba47b
scala> res6.mkString
res10: String = 1234560
scala> res6.mkString(",")
res11: String = 1,2,3,4,5,6,0
scala> res6.mkString("<",",",">")
res12: String = <1,2,3,4,5,6,0>
2. 变长数组
定长数组指的是数组的长度不变,而变长数组指的是长度可以变。变长数组使用scala.collection.mutable.ArrayBuffer 来导包实现,如下代码:
//在客户端 如果没有导包会出现找不到类异常
scala> new ArrayBufferInt
<console>:12: error: not found: type ArrayBuffer
new ArrayBufferInt
^
scala> import scala.collection.mutable.ArrayBuffer
//创建一个空的变长数组
scala> new ArrayBufferInt
res16: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
变长数组的用法与定长用法一致,只不过多了对元素的增减操作:
//可以单独添加一个元素
scala> res16 += 3
scala> res16 += 7
scala> res16 += 10
res19: res16.type = ArrayBuffer(3, 7, 10)
//也可以添加多个元素
scala> res16 += (4,5,6)
res20: res16.type = ArrayBuffer(3, 7, 10, 4, 5, 6)
//也可以添加一个数组
scala> res16 ++= Array(4,5,6)
res21: res16.type = ArrayBuffer(3, 7, 10, 4, 5, 6, 4, 5, 6)
//移除数组元素
scala> res16.remove(0)
res23: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(7, 10, 4, 5, 6, 4, 5, 6)
scala> res16.remove(0,3)
res25: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(5, 6, 4, 5, 6)
//修改元素
scala> res16(2) = 6
res27: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(5, 6, 6, 5, 6)
3. 变长数组转为定长数组
如下展示了变长数组转为定长数组:
scala> ArrayBuffer("hadoop","spark","storm")
scala> res0.toArray
res1: Array[String] = Array(hadoop, spark, storm)
4. List&ListBuffer
4.1 不可变List操作用如下:
//如果想创建一个空List 可以使用Nil来实现
scala> Nil
res0: scala.collection.immutable.Nil.type = List()
//也可以使用伴生对象的apply来实现创建
scala> val list1 = List(1,2,3,4,5)
list1: List[Int] = List(1, 2, 3, 4, 5)
//.head可以获取List的头部
scala> list1.head
res1: Int = 1
//.tail可以获取List的尾部(这里的尾部就是除了头部以外的所有其他元素)
scala> list1.tail
res2: List[Int] = List(2, 3, 4, 5)
//将一个对象加入到后面列表的头部
scala> val list2 = 1 :: Nil
list2: List[Int] = List(1)
scala> val list2 = 1 :: 2 :: 3 :: Nil
list2: List[Int] = List(1, 2, 3)
scala> val list3 = 55 :: list2
list3: List[Int] = List(55, 1, 2, 3)
4.2 ListBuffer 的操作与 ArrayBuffer 的操作思想差不多。代码如下:
scala> import scala.collection.mutable.ListBuffer
//创建ListBuffer
scala> new ListBuffer[Int]
//添加一个元素
scala> res4 += 2
scala> res4 += 3
res6: res4.type = ListBuffer(2, 3)
//添加多个元素
scala> res4 += (2,5,6,7,4)
res7: res4.type = ListBuffer(2, 3, 2, 5, 6, 7, 4)
//添加一个队列
scala> res4 ++= List(9,9,9)
res9: res4.type = ListBuffer(2, 3, 2, 5, 6, 7, 4, 9, 9, 9)
//删除元素
scala> res4 -= 2
res11: res4.type = ListBuffer(3, 2, 5, 6, 7, 4, 9, 9, 9)
scala> res4 -= (9,9)
res12: res4.type = ListBuffer(3, 2, 5, 6, 7, 4, 9)
scala> res4 --= List(5,6,7)
res13: res4.type = ListBuffer(3, 2, 4, 9)
//变长ListBuffer转定长List
scala> res4.toList
res14: List[Int] = List(3, 2, 4, 9)
//变长ListBuffer转定长Array
scala> res4.toArray
res15: Array[Int] = Array(3, 2, 4, 9)
5. 可变&不可变Map
以示例驱动为准,如下是对不可变Map的操作:
// 创建一个Map对象
scala> Map("xiaoming" -> 85, "xiaohong" -> 99)
res1: scala.collection.immutable.Map[String,Int] = Map(xiaoming -> 85, xiaohong -> 99)
// 取值操作
scala> res1("xiaoming") // res2: Int = 85
// 不可变Map是无法插入新值的
scala> res1("xiaozhang") = 77
<console>:13: error: value update is not a member of scala.collection.immutable.Map[String,Int]
res1("xiaozhang") = 77
// Map常见的两种遍历模式:
scala> for ((k1,v1) <- res1 ) println(k1+" : "+v1)
xiaoming : 85
xiaohong : 99
scala> for ( k1 <- res1.keySet ) println(k1+" : "+ res1(k1))
xiaoming : 85
xiaohong : 99
为了解决扩展Map的需求,就需要使用可变的Map,使用如下:
//创建可变Map的两种方式,HashMap是Map的一个子类实现
scala> scala.collection.mutable.Map("xiaoming" -> 85)
res6: scala.collection.mutable.Map[String,Int] = Map(xiaoming -> 85)
scala> scala.collection.mutable.HashMapString,Int
res9: scala.collection.mutable.HashMap[String,Int] = Map()
//对可变Map进行存值操作
scala> res6("xiaohong") = 95
scala> res6
res8: scala.collection.mutable.Map[String,Int] = Map(xiaohong -> 95, xiaoming -> 85)
对于取值,这里需要注意的是默认情况下,如果找不到对应的key是会报错的:
scala> res6("xiaoli")
java.util.NoSuchElementException: key not found: xiaoli
at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:59)
at scala.collection.mutable.HashMap.apply(HashMap.scala:65)
... 32 elided
scala> res6.getOrElse
getOrElse getOrElseUpdate
scala> res6.getOrElse("xiaoli",-1)
res12: Int = -1
对于取的操作,有可能去得到值有可能取不到;当调用get方法的时候返回的有两种情况,我们称之为Option,当取得到值返回Some,当取不到值返回None:
scala> Map("xiaoming" -> 85, "xiaozhang" -> 96)
res0: scala.collection.immutable.Map[String,Int] = Map(xiaoming -> 85, xiaozhang -> 96)
scala> res0.get("xiaoli")
res2: Option[Int] = None
scala> res0.get("xiaozhang")
res3: Option[Int] = Some(96)
// 当不确定返回Some还是None时为进一步取到具体的值
scala> res0.get("xiaozhang1").getOrElse(-1)
res6: Int = -1
6. Tuple元组简单应用
与列表一样,元组也是不可变的,但与列表不同的是元组可以包含不同类型的元素。元组的值是通过将单个的值包含在圆括号中构成的。例如:
scala> val myTuple = (1,2,3,'a','b','c')
myTuple: (Int, Int, Int, Char, Char, Char) = (1,2,3,a,b,c)
scala> myTuple._4
res0: Char = a
// 元组的元素是无法赋值的
scala> myTuple._4 = 'k'
<console>:12: error: reassignment to val
想获取更多技术视频,请前往叩丁狼官网:http://www.wolfcode.cn/openClassWeb_listDetail.html
网友评论