在Groovy中有什么方法可以做到这一点吗?

在Groovy中有什么方法可以做到这一点吗?,groovy,Groovy,我想要一个返回列表的代码,如下所示: def list = ["RR","SS"] //code to get the output as [R,R,S,S] 我想出了这样的主意: def Ash = ["RR","as","RTY"] def listA = [] for(i=0;i<Ash.size();i++) { listA << Ash[i].collect{ it as String } } AshNew = listA.flatten() print

我想要一个返回
列表的代码,如下所示:

def list = ["RR","SS"]
//code to get the output as [R,R,S,S]
我想出了这样的主意:

def Ash = ["RR","as","RTY"]
def listA = []
for(i=0;i<Ash.size();i++)
{ 
    listA <<  Ash[i].collect{ it as String }
}
AshNew = listA.flatten()
println AshNew // this prints [R, R, a, s, R, T, Y] which is what i needed..
def Ash=[“RR”、“as”、“RTY”]
def listA=[]
对于(i=0;i

Ash.join().split("").tail()

更好吗?

怎么样:

Ash.join().findAll()

findAll返回每一个非空的元素,对于一个连接的字符串,它是所有元素。

这样打印
[,S,R,p,p]
如果列表中的第一个
被省略就好了!修复了这个问题,删除了第一个元素谢谢你做得很好!我想在看到你的解决方案后,我还需要更加常规:Dhow this works?首先如果我给出
Ash.join()
它返回
SRPP
现在如何通过将参数传递为
“在
SRPP
之间应用
split
方法。split
获取一个regexp。regexp
匹配每个字符之间的每个间隔。但是,忽略
P
\n
之间的间隔有点奇怪:(哦
收集
完成了所有元素的迭代工作!天啊,我完全忘记了这一点!:d扩展运算符收集:Ash*.asType(List).flatte()我的版本肯定更密集。我添加了另一个更简单语法+1的答案,这是一个很好的选择。还可以使用
Ash.join().toList()
,这可能在其工作中有点不透明
Ash.join().findAll()