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
- scala基础7-数组进阶
- scala基础5-函数进阶
- scala基础-数组
- Scala进阶源码实战之一——数组、函数基础、流程控制、文件、异常
- Scala教程(四)数组进阶实战
- scala基础4-for循环进阶
- scala基础8-map/tuple/zip进阶
- scala基础20-包使用进阶
- scala基础30-模式匹配进阶
- Scala数组的基本操作,数组进阶操作,多维数组
- Scala基础(4):数组
- scala基础语法-2数组
- scala进阶7-类型约束
- Scala进阶
- Scala进阶
- scala进阶
- Scala基础—数组操作示例
- Scala 基础2⃣️数组
- shell 常用命令行
- Spring的JdbcTemplate
- 01-分数规划专题
- 理清浏览器Window对象&window对象&this对象
- java-基础-数组探究
- scala基础7-数组进阶
- POJ 1677 Girls' Day
- QT 学习
- 欢迎使用CSDN-markdown编辑器
- 游泳
- 73. Set Matrix Zeroes
- C++中的map和multimap容器
- Spring MVC 实现RESTful 返回JSON格式数据
- Hadoop YARN公平调度(FairScheduler)介绍