每次复选框都会点击相应的id以传递给check方法..
@RequestMapping(value = Array("checkBoxCheck.html"))
@ResponseBody
def check(@RequestParam checkBoxId: Long) {
processing(checkBoxId)
}
def processing(checkBoxId:Long){....}
上面的代码是spring scala的例子.. 我想将每个id添加到列表中,如果它已经存在,则已经从处理方法
中的列表中删除它答案 0 :(得分:1)
这样的事情:
def processing(id: Long): List[Long] = {
if(list.contains(id))
list.filter(_ != id)
else
list :+ id
}
val list = List(1,2,3)
scala> processing(3)
res0: List[Long] = List(1, 2)
scala> processing(4)
res1: List[Long] = List(1, 2, 3, 4)
答案 1 :(得分:1)
'模式匹配'用例
def processing(list: List[Int] , id: Int): List[Int] = list match {
case list if list.contains(id) => list filterNot(_ == id)
case _ => list :+ id
}
val list = List(1, 2, 3) //> list : List[Int] = List(1, 2, 3)
processing(list, 3) //> res0: List[Int] = List(1, 2)
processing(list, 4) //> res1: List[Int] = List(1, 2, 3, 4)