自学内容网 自学内容网

用Scala中的Array解决问题

package test29
import scala.collection.mutable.ArrayBuffer
// 1
case class DreamItem(content:String,var isDone:Boolean,deadline:String,var order:Int)
object Test29_pro {
  def main(args: Array[String]): Unit = {
    // 2
    val dreamList = ArrayBuffer[DreamItem]()  // 梦想清单
    // 3
    val dream1 = DreamItem("世界和平",false,"2024-11-19",1)
    val dream2 = DreamItem("有一架私人飞机",false,"2024-11-19",2)
    val dream3 = DreamItem("有十辆跑车",false,"2026-10-19",6)

    dreamList += dream1
    dreamList += dream2
    dreamList += dream3

    dreamList += DreamItem("中彩票(100万以上)",false,"2024-11-20",2)
    dreamList += DreamItem("周游全世界",false,"2030-10-01",1)
    dreamList += DreamItem("吃遍全宇宙",false,"2040-10-01",1)
    dreamList += DreamItem("有100套房子",false,"2030-10-01",1)
    dreamList += DreamItem("莫名其妙获得10000000000000",false,"2030-10-01",1)
    dreamList += DreamItem("月薪十万+",false,"2030-10-01",1)
    dreamList += DreamItem("躺着就有钱",false,"2044-10-01",1)
    println(dreamList)
    // 4
//    dreamList(1) = dreamList(1).copy(isDone = true)
    dreamList(1).isDone = true
    println(dreamList)
    // 5
//    dreamList(2) = dreamList(2).copy(order = 4)
    dreamList(2).order = 100
    println(dreamList)
    // 6
    val list = dreamList.sortWith((a,b)=> a.order > b.order)
    // 7
    val newList = list.slice(0,5)
    // 8
    list.foreach(e => {
      println(s"${e.content} \t\t ${e.isDone} \t ${e.order}")
    })
//    dreamList.sortWith(_.order > _.order)
//    println(dreamList)
  }
}


原文地址:https://blog.csdn.net/2401_82368706/article/details/143888628

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!