scala基础7-数组进阶

本文深入探讨Scala中数组(Array)及缓冲区(ArrayBuffer)的使用技巧,包括初始化、操作方法如添加、移除元素等,并介绍如何通过过滤(filter)、映射(map)等函数式编程特性高效处理数据。
object InAction {
    val nums  = new Array[Int](10)                //> nums  : Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    val a = new Array[String](10)                 //> a  : Array[String] = Array(null, null, null, null, null, null, null, null, nu
                                                  //| ll, null)
    val s = Array("hello", " world")              //> s  : Array[String] = Array(hello, " world")
    s(0) = "goodbye"
    for(value <- s) print(value)                  //> goodbye world

    import scala.collection.mutable.ArrayBuffer
    val b = ArrayBuffer[Int]()                    //> b  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
    b += 1                                        //> res0: InAction.b.type = ArrayBuffer(1)
    b += (1, 2, 3, 5)                             //> res1: InAction.b.type = ArrayBuffer(1, 1, 2, 3, 5)
    b ++= Array(8, 15) //两个加号                     //> res2: InAction.b.type = ArrayBuffer(1, 1, 2, 3, 5, 8, 15)
    b                                             //> res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 5,
                                                  //|  8, 15)

    b.trimEnd(5) //切除后面5个元素
    b                                             //> res4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1)
    b.insert(2,6) //在第2个元素后面插入6
    b                                             //> res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 6)
    b.insert(3,7,8,9) //在第3个元素后面插入7,8,9
    b                                             //> res6: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 6, 7, 8,
                                                  //|  9)

    b.remove(2) //删除第2个元素后面的1个元素                  //> res7: Int = 6
    b                                             //> res8: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 7, 8, 9)
                                                  //| 
    b.remove(2, 3) //删除第2个元素后面的3个元素
    b                                             //> res9: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1)
    var c = b.toArray                             //> c  : Array[Int] = Array(1, 1)
    //var tmp = 0
    for(i <- 0 until c.length)   println(c(i))    //> 1
                                                  //| 1
                                                  
    val d = Array(2, 3, 4, 5, 7)                  //> d  : Array[Int] = Array(2, 3, 4, 5, 7)
    //yield获得一个集合
    val result = for (elem <- d) yield 2 * elem   //> result  : Array[Int] = Array(4, 6, 8, 10, 14)
    val result2 = for (elem <- d if elem % 2 == 0) yield 2 * elem
                                                  //> result2  : Array[Int] = Array(4, 8)
    //生产环境下写法
    d.filter(_ % 2 == 0).map(_ * 2)               //> res10: Array[Int] = Array(4, 8)
    //复杂写法,看起来不专业
    d.filter(elem => elem % 2 == 0).map(elem => elem * 2)
                                                  //> res11: Array[Int] = Array(4, 8)
    //求和
    Array(1,2,5,7).sum                            //> res12: Int = 15
    
    //获取最大值
    import scala.collection.mutable.ArrayBuffer
    ArrayBuffer("hello", "ddddd", "a", "little").max
                                                  //> res13: String = little
    //排序,默认升序
    var e = ArrayBuffer(1,7,2,9)                  //> e  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 7, 2, 9)
    val bSorted = e.sorted                        //> bSorted  : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 7,
                                                  //|  9)
    //使用快速排序,改变的是e本身
    //scala.util.Sorting.quickSort(e)
    
    //元素之间用and相连
    e.mkString(" and ")                           //> res14: String = 1 and 7 and 2 and 9
    e.mkString("<", ",", ">")                     //> res15: String = <1,7,2,9>
    
    //定义矩阵(二维数组)
    val matrix = Array.ofDim[Double](3, 4)        //> matrix  : Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0
                                                  //| , 0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0, 0.0))
    matrix(2)(1) = 42
    matrix                                        //> res16: Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0, 0
                                                  //| .0, 0.0, 0.0), Array(0.0, 42.0, 0.0, 0.0))
    
    val f = new Array[Array[Int]](10)             //> f  : Array[Array[Int]] = Array(null, null, null, null, null, null, null, nu
                                                  //| ll, null, null)
    for (i <- 0 until f.length)
    	f(i) = new Array[Int](i + 1)
    f                                             //> res17: Array[Array[Int]] = Array(Array(0), Array(0, 0), Array(0, 0, 0), Arr
                                                  //| ay(0, 0, 0, 0), Array(0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0, 
                                                  //| 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0
                                                  //| , 0), Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值