我们知道,通过each可以非常方便的遍历数组或列表中的元素,那么each的过程,是能够被中止的吗?
通过示例来研究一下:
//在list中找到一个元素
int target = 2
def list = 1..5
list.each{
println it
if(it == target) {
return true; //no use
}
}
可以发现,通过return true等方法,都不能中止each的继续执行。
要想中止,需要用find
list.find{
println it
if(it == target) {
return true
}
}
所以,如果find总是返回false,那么和each是一样的。