List 删除列表中匹配项之前的项groovy

List 删除列表中匹配项之前的项groovy,list,groovy,List,Groovy,如果我有这样的清单: def list = ['a','b','c','d','c'] 我想删除第一个c之前的所有项目,如何使用groovy轻松地完成这项工作 如果存在,我正在寻找一行答案:)。我当前的解决方案: def start = list.findIndexOf { it ==~ /(?i)c/ } commands = list[start..-1] 我可以写一行,但可读性不强 def list = ['a', 1, 2, 3, 'c', 'b', 'c', 'd', 'c']

如果我有这样的清单:

def list = ['a','b','c','d','c']
我想删除第一个c之前的所有项目,如何使用groovy轻松地完成这项工作

如果存在,我正在寻找一行答案:)。

我当前的解决方案:

def start = list.findIndexOf { it ==~ /(?i)c/ }
commands = list[start..-1]
我可以写一行,但可读性不强

def list = ['a', 1, 2, 3, 'c', 'b', 'c', 'd', 'c']

assert ['c', 'b', 'c', 'd', 'c'] == list.dropWhile{it != 'c'}
assert ['c', 'b', 'c', 'd', 'c'] == list.drop(list.indexOf('c'))
assert ['c', 'b', 'c', 'd', 'c'] == list[list.indexOf('c')..-1]    
assert ['c', 'b', 'c', 'd', 'c'] == list.subList(list.indexOf('c'), list.size())
assert ['a', 1, 2, 3, 'c', 'b', 'c', 'd', 'c'] == list
列表
始终保留。你每次都会得到一份新的名单