美文网首页
Scala-数组、映射

Scala-数组、映射

作者: 小猪Harry | 来源:发表于2018-10-11 21:15 被阅读0次

    1、数组
    1)、创建数组
    创建定长数组和长度可变数组

    //创建定长数组,使用new关键字,指定数组泛型为Int,长度为5,默认会初始化这5个值为指定类型的默认值,比如Int的话则5个默认值都是0
    //注意:如果new,相当于调用了数组的apply方法,直接为数组赋值
    scala> val arr = new Array[Int](5)
    arr: Array[Int] = Array(0, 0, 0, 0, 0)
    //修改数组元素的值
    scala> arr(0)
    res16: Int = 0
    
    scala> arr(0) = 1
    
    scala> arr(1) = 2
    
    scala> arr(2) = 3
    
    scala> arr
    res20: Array[Int] = Array(1, 2, 3, 0, 0)
    
    //直接创建定长数组并且初始化元素
    scala> val arr2 = Array(1,2,3,4,5)
    arr2: Array[Int] = Array(1, 2, 3, 4, 5)
    
    scala> arr2
    res21: Array[Int] = Array(1, 2, 3, 4, 5)
    
    //打印数组,默认是打印hashcode,使用toBuffer则会打印数组元素
    scala> println(arr)
    [I@6c06b1bc
    
    scala> println(arr.toBuffer)
    ArrayBuffer(1, 2, 3, 0, 0)
    
    //注意以下因为有没有使用new的区别
    scala> val a = Array[Int](10)
    a: Array[Int] = Array(10)
    a.length = 1
    scala> val b = new Array[Int](10)
    b: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    a.length = 10
    
    //创建变成数组(使用数组缓存ArrayBuffer)
    scala> val ab = new ArrayBuffer[Int]()
    //提示早不到ArrayBuffer类型,需要导包
    <console>:7: error: not found: type ArrayBuffer
           val ab = new ArrayBuffer[Int]()
                        ^
    //按制表符键会给出提示
    scala> import scala.collection.mutable.Array
    ArrayBuffer    ArrayBuilder   ArrayLike      ArrayOps       ArraySeq       ArrayStack
    
    //导包完成
    scala> import scala.collection.mutable.ArrayBuffer
    import scala.collection.mutable.ArrayBuffer
    
    //创建变长数组new ArrayBuffer[Int]()或者不需要new关键字
    ArrayBuffer[Int](),既然是变长那么就不需要指定长度了
    scala> val ab = new ArrayBuffer[Int]()
    ab: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
    
    //向数组中加入单个或多个元素 +=
    
    scala> ab += 1
    res24: ab.type = ArrayBuffer(1)
    
    scala> ab += 2
    res25: ab.type = ArrayBuffer(1, 2)
    
    //加入多个元素
    scala> ab += (3,4,5)
    res26: ab.type = ArrayBuffer(1, 2, 3, 4, 5)
    
    scala> ab
    res27: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
    

    2)、数组操作

    //数组相加变长和定长的也可以相加 使用 ++=
    //两个变长数组相加,ac中元素加入到ab中
    scala> ab ++= ac
    res32: ab.type = ArrayBuffer(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
    
    scala> ab
    res33: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
    
    //将定长数组元素加入到变长数组ac中
    scala> ac ++= Array(6,7,8)
    res34: ac.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8)
    
    scala> ac
    res35: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8)
    
    //数组指定位置插入元素,使用insert
    //在ac数组的角标为1的位置插入两个9
    scala> ac.insert(1,9,9)
    
    scala> ac
    res38: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 9, 9, 2, 3, 4, 5, 6, 7, 8)
    
    //ac角标为0的位置插入两个9
    scala> ac.insert(0,9,9)
    
    scala> ac
    res40: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(9, 9, 1, 9, 9, 2, 3, 4, 5, 6, 7, 8)
    
    //数组指定位置删除元素,使用remove
    //ac的角标为0的位置删除2个元素
    scala> ac.remove(0,2)
    
    scala> ac
    res42: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 9, 9, 2, 3, 4, 5, 6, 7, 8)
    
    scala> ac.remove(1,2)
    
    scala> ac
    res44: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8)
    

    3)、遍历数组
    使用for循环以及until
    好用的until会生成脚标,0 until 10 包含0不包含10

    scala> 0 until 10
    res47: scala.collection.immutable.Range = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    
    //变量数组
    scala> for(i <- 0 until ac.length) print (ac(i) + " ")
    1 2 3 4 5 6 7 8
    

    4)、数组转换
    yield关键字将原始的数组进行转换会产生一个新的数组,原始的数组不变

    scala> val arr = Array(1,2,3,4,5,6,7,8,9)
    arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
    
    //将arr中每个元素乘以10放到一个新的数组中
    scala> val arr2 = for (i <- arr) yield i * 10
    arr2: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)
    
    scala> arr2
    res0: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)
    
    //而使用map更方便
    scala> val arr3 = arr.map(_ * 10)
    arr3: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)
    
    scala> arr3
    res2: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)
    或者
    scala> val arr4 = arr.map(x => x * 10)
    arr4: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)
    

    使用过滤器

    scala> val arr2 = for( i <- arr if i % 2 == 0) yield i * 2
    arr2: Array[Int] = Array(4, 8, 12, 16)
    //对于上面这种情况直接使用map就不好使了,因为带有过滤条件,所以这里使用过滤器filter
    scala> val arr5 = arr.filter(x => x % 2 ==0)
    arr5: Array[Int] = Array(2, 4, 6, 8)
    
    scala> val arr5 = arr.filter(x => x % 2 ==0).map(x => x * 2)
    arr5: Array[Int] = Array(4, 8, 12, 16)
    
    scala> arr5
    res6: Array[Int] = Array(4, 8, 12, 16)
    
    //或者使用_这种更简便的写法
    scala> val arr6 = arr.filter(_ % 2 ==0).map(_ * 2)
    arr6: Array[Int] = Array(4, 8, 12, 16)
    
    scala> arr6
    res7: Array[Int] = Array(4, 8, 12, 16)
    

    5)、数组常用算法

    //数组最大值、最小值、求和
    scala> arr.sum
    res18: Int = 41
    
    scala> arr.max
    res19: Int = 9
    
    scala> arr.min
    res20: Int = 1
    
    //数组排序的一些方法
    scala> val arr = Array(3,1,6,8,5,4,9,2,3)
    arr: Array[Int] = Array(3, 1, 6, 8, 5, 4, 9, 2, 3)
    
    scala> arr.sorted
    res9: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 8, 9)
    
    scala> arr
    res10: Array[Int] = Array(3, 1, 6, 8, 5, 4, 9, 2, 3)
    
    scala> val arr2 = arr.sorted
    arr2: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 8, 9)
    
    scala> arr2
    res11: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 8, 9)
    
    scala> val arr2 = arr.sorted.reverse
    arr2: Array[Int] = Array(9, 8, 6, 5, 4, 3, 3, 2, 1)
    
    scala> arr2
    res12: Array[Int] = Array(9, 8, 6, 5, 4, 3, 3, 2, 1)
    
    scala> arr.sortBy(x => x)
    res13: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 8, 9)
    
    scala> arr.sortWith(_>_)
    res14: Array[Int] = Array(9, 8, 6, 5, 4, 3, 3, 2, 1)
    
    scala> arr.sortWith(_<_)
    res15: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 8, 9)
    
    scala> arr.sortWith((x,y)=> x<y)
    res16: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 8, 9)
    
    scala> arr.sortWith((x,y)=> x>y)
    res17: Array[Int] = Array(9, 8, 6, 5, 4, 3, 3, 2, 1)
    

    2、映射
    在Scala中,把哈希表这种数据结构叫做映射

    1)、创建映射

    //第一种创建方式:使用"->" a -> b中好比a是键b是值
    scala> val props = Map(1 -> 23,2 -> 45)
    //用java的话来说就是键和值类型都是Int
    props: scala.collection.immutable.Map[Int,Int] = Map(1 -> 23, 2 -> 45)
    scala> val props = Map("user" -> "admin", "pwd" -> "1234")
    //用java的话来说就是键和值类型都是String
    props: scala.collection.immutable.Map[String,String] = Map(user -> admin, pwd -> 1234)
    
    //第二种创建方式:使用(k,v)
    scala> val map = Map((1,"tom"),(2,"jack"))
    map: scala.collection.immutable.Map[Int,String] = Map(1 -> tom, 2 -> jack)
    
    scala> val name = map(1)
    name: String = tom
    

    2、获取和修改映射中的值

    //根据键取值
    scala> val name = map(1)
    name: String = tom
    //修改值发现不行,因为上面的Map是不可变的,需要重新导包
    scala> map(1) = "lucy"
    <console>:9: error: value update is not a member of scala.collection.immutable.Map[Int,String]
                  map(1) = "lucy"
    
    //重新导入值可变的map包就能修改元素成功了
    scala> import scala.collection.mutable.Map
    Map          MapBuilder   MapLike      MapProxy
    
    scala> import scala.collection.mutable.Map
    import scala.collection.mutable.Map
    
    scala> val map = Map((1,"tom"),(2,"jack"))
    map: scala.collection.mutable.Map[Int,String] = Map(2 -> jack, 1 -> tom)
    
    scala> map(1) = "lucy"
    
    scala> map
    res25: scala.collection.mutable.Map[Int,String] = Map(2 -> jack, 1 -> lucy)
    
    //往映射中添加元素
    scala> map += ((3,"wangw"),(4,"lining"))
    res26: map.type = Map(2 -> jack, 4 -> lining, 1 -> lucy, 3 -> wangw)
    
    scala> map += (5 -> "xiaox",6 -> "jiej")
    res27: map.type = Map(2 -> jack, 5 -> xiaox, 4 -> lining, 1 -> lucy, 3 -> wangw, 6 -> jiej)
    
    //加的时候如果k一样那么会覆盖v,发5对应的xiaox被修改成了xiao
    scala> map += (5 -> "xiao")
    res28: map.type = Map(2 -> jack, 5 -> xiao, 4 -> lining, 1 -> lucy, 3 -> wangw, 6 -> jiej)
    
    //两个映射相加 ++=
    scala> val map2 = Map((1,"laowang"),(2,"lisi"))
    map2: scala.collection.mutable.Map[Int,String] = Map(2 -> lisi, 1 -> laowang)
    
    scala> val map3 = Map(3 ->"jinbm")
    map3: scala.collection.mutable.Map[Int,String] = Map(3 -> jinbm)
    
    //相加的时候如果k一样则v会覆盖,map3中元素加到map2中
    scala> map2 ++= map3
    res33: map2.type = Map(2 -> lisi, 1 -> laowang, 3 -> jinbm)
    
    scala> map2
    res34: scala.collection.mutable.Map[Int,String] = Map(2 -> lisi, 1 -> laowang, 3 -> jinbm)
    

    注:如果映射中根据k不能取得v那么会报错

    scala> map2(4)
    java.util.NoSuchElementException: key not found: 4
            at scala.collection.MapLike$class.default(MapLike.scala:228)
            at scala.collection.AbstractMap.default(Map.scala:58)
            ...
    
    //使用getOrElse来解决,如果找不到这个k对应的v那么给它赋个默认值
    scala> map2.getOrElse(4,0)
    res36: Any = 0
    

    打印水仙花数

    (100 to 999).filter(x => {pow(x%10,3) + pow(x/10%10,3) + pow(x/100,3) == x}).foreach(println)
    

    相关文章

      网友评论

          本文标题:Scala-数组、映射

          本文链接:https://www.haomeiwen.com/subject/yfyiaftx.html