我正在尝试从列表中查找并显示多个数据(使用groovy),但是要显示的项目取决于找到的出现次数。
我尝试过这样的事情(下面的代码)
我还尝试了其他方法来使用size()
,length()
查找集合的大小,但没有任何效果。
list[list.findIndexValues { it == 'PA1-03-22'}.collect { it.count == 1 ? it+2 : it+1}]
现在,我总是会遇到关于计数的错误(无签名方法...),但是当然目标是根据出现的次数来获取适当的项。
答案 0 :(得分:0)
从您的评论中我发现您想要实现这一目标:
def list = [
"PA1-03-22", "last_name_1", "first_name_1",
"PA1-03-23", "last_name_2", "first_name_2",
"PA1-03-22", "last_name_3", "first_name_3"
]
listnew = list.findIndexValues { it == 'PA1-03-22' }
listlast = []
if (listnew.size() == 1) {
Integer index = listnew[0] + 1
listlast = list[index]
} else {
listnew.each {
def element = list[it + 1]
listlast << element
}
}
问题在于您的代码这一部分中的it
:
.collect { it.count == 1 ? it+2 : it+1}]
指的是以下元素:
list.findIndexValues { it == 'PA1-03-22' }
即[0]
这就是为什么您得到错误。
我还没有使用过iReport,但是您可以通过以下方式将其包装在闭包中:
def result = {
def list = [
"PA1-03-22", "last_name_1", "first_name_1",
"PA1-03-23", "last_name_2", "first_name_2",
"PA1-03-24", "last_name_3", "first_name_3"
]
listnew = list.findIndexValues { it == 'PA1-03-22' }
listlast = []
if (listnew.size() == 1) {
Integer index = listnew[0] + 1
listlast = list[index]
} else {
listnew.each {
def element = list[it + 1]
listlast << element
}
}
listlast
}
println(result())