该列表可以为空。我想要做 :
def value = "";
def list = getList()
if (!list.isEmpty()){
value = list.first().foo
}
例如我发现了这种方式:
assert ( [].find()?.foo?:"empty" ) == "empty"
assert ([[foo:"notEmpty1"], [foo:"notEmpty2"]].find()?.foo?:"empty") == "notEmpty1"
有没有更好的办法 ?
谢谢!:)
编辑:
我通过使用得到了很好的答案[0]
assert ( [][0]?.foo?:"empty" ) == "empty"
assert ([[foo:"notEmpty1"], [foo:"notEmpty2"]][0]?.foo?:"empty") == "notEmpty1"