scala基础7-数组进阶

来源:互联网 发布:楚天数控编程代码 编辑:程序博客网 时间:2024/05/17 06:32
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))}

0 0