0

每次单击复选框时,相应的 id 都会传递给检查方法。

  @RequestMapping(value = Array("checkBoxCheck.html"))
      @ResponseBody
      def check(@RequestParam checkBoxId: Long) {
      processing(checkBoxId) 

      }

    def processing(checkBoxId:Long){....}

上面的代码是spring scala的示例。我想将每个id添加到一个列表中,如果它已经存在,则在处理方法中将它从列表中删除

4

2 回答 2

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)
于 2013-06-07T08:31:17.163 回答
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)
于 2013-06-07T05:20:08.630 回答