首页 技术 正文
技术 2022年11月16日
0 收藏 999 点赞 2,117 浏览 2110 个字

Iterable 是序列(Seq), 集(Set) 映射(Map)的特质

序列式有序的集合如数组和列表

集合可以通过== 方法确定对每个对象最多包含一个

映射包含了键值映射关系的集合

列表缓存:

  使用ListBuffer代替List 另一个理由是为了避免栈溢出的风险

数组缓存: ArrayBuffer需要先从可变集合包引用 scala.collection.mutable.ArrayBuffer

  val buf = new ArrayBuffer[Int]()

队列Queue:先进先出

class BankAccount {
private var bal: Int = 0
def balance: Int = bal
def deposit(account: Int){
require(account > 0)
bal += account
}
def widthDraw(account: Int): Boolean = {
if (account > bal ) false
else {bal -= account
true }
}
}
abstract  class Simulation {
type Action = {} => Unit
case class WorkItem(time: Int, action: Action)
private var currtime = 0
def currentTime: Int = currtime private var agenda: List[WorkItem] = List()
private def insert(arg: List[WorkItem],
item: WorkItem) : List[WorkItem] = {
if (arg.isEmpty || item.time < arg.head.time) item :: arg
else arg.head :: insert(arg.tail, item)
}
def afterDelay(delay: Int)(block: => Unit) = {
/* val item = WorkItem(currentTime + delay, { } => block)
agenda = insert(agenda, item )*/
} private def next(){
(agenda: @unchecked) match{
case item:: rest =>
agenda = rest
currtime = item.time
item.action()
}
}
def run(){
afterDelay(0){
println("*** sim start... time = + currentTime +***")
}
// while (!agenda.isEmpty) next()
}
}
class Time {
private[tjis] var h = 12
private[this] var n = {}
def hour: Int = h
def hour_ = (x: Int) {h = x}
def minute: Int = m
def minute_ = (x: Int) {m = x}}
class scala {}
object scala{
def main(args: Array[String]) {
System.out.println("HelloWorld")
}
def isort(sx: List[Int]): List[Int] = {
if (sx.isEmpty) Nil else isinsert(sx.head, isort(sx.tail))
} def isinsert(ss: Int,sx: List[Int]) : List[Int] = {
if (sx.isEmpty || ss <= sx.head) ss :: sx else sx.head :: isinsert(ss, sx.tail)
}
// 以下使用模式匹配
def isort2(sx: List[Int]) : List[Int] = sx match {
case List() => List()
case x :: sxl => insert2(x, isort2(sxl))
} def insert2(x: Int, xs: List[Int]): List[Int] = xs match {
case List() => List(x)
case y :: ys => if (x <= y) x:: xs else y :: insert2(x, ys)
} def append1[T](sx: List[T], ys: List[T]) : List[T] ={
sx match {
case List() => ys
// case x :: sxl => x :: append1(sxl, ys)
}
}
//队列
val queue1 = new mutable.Queue[Int]
val va1 = queue1.enqueue(1) // 不可变队列添加元素用enQueue
val queue = new mutable.Queue[String]
queue += "a"
queue ++= List("b", "c") //有序的
val ts = mutable.TreeSet(2,4,6,7,0,8,3)
def mapMaker: Map[String, String] = {
new HashMap[String, String] with
SynchronizedMap[String, String] {
// override def default1(key: String) = " hellon word"
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848