最典型的的多维数组就是二维数组。
object test3 {
def main(args: Array[String]): Unit = {
//二维数组
//2.1 创建方式1
val arr5 = Array.ofDim[Int](2, 2) //2行2列
//2.2 访问数组的元素
arr5(0)(0) = 1
arr5(0)(1) = 2
arr5(1)(0) = 3
arr5(1)(1) = 4
//2.3 把一维数组转化成二维数组
var arr6 = Array(1, 2, 3, 4, 5, 6)
var arr7 = arr6.grouped(3) //3个元素一组
// println(arr7)
while (arr7.hasNext) {
var rs = arr7.next()
println(s"第一个元素是:${rs(0)}")
rs.foreach(println)
// println(arr7.next())
println("------------")
}
}
}
任务:梦想管理清单
package test2
import scala.collection.mutable.ArrayBuffer
//1.
case class DreamItem(content:String,var isDone:Boolean,deadline:String,var order:Int)
object testarray2 {
def main(args: Array[String]): Unit = {
// 2.
val dreamList = ArrayBuffer[DreamItem]() //梦想清单
//3.
val dream1 = DreamItem("走遍全世界",true,"2010-10-10",1)
val dream2 = DreamItem("吃遍全世界的美食",true,"2020-7-24",2)
val dream3 = DreamItem("健健康康的过完一生",true,"2024-11-19",3)
dreamList += dream1
dreamList += dream2
dreamList += dream3
dreamList += DreamItem("学会独立",true,"2024-1-12",1)
dreamList += DreamItem("一夜暴富",false,"2018-4-23", 1)
// println(dreamList)
// 4.
dreamList(1).isDone = true
//5.
dreamList(2).order = 100
//6.
val list = dreamList.sortBy( _.order)
//7.
val newList = list.slice(0,5)
//8.
list.foreach(e => {
println(s"${e.content}\t\t${e.isDone}\t${e.order}")
})
}
}