groovy - 有没有办法在 Groovy 中做到这一点?

标签 groovy

我想要一个返回 list 的代码像这样 :

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..

但是我仍然想知道我们是否可以使用另一种方式在 Groovy 中做类似的事情?由于我是 Groovy 的新手,我想了解更多 Groovier 解决方案!感谢您的回答!

最佳答案


AshNew = Ash.collect { it as List }.flatten()

好点?

关于groovy - 有没有办法在 Groovy 中做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6682114/

相关文章:

sql - 在 Grails 中使用 groovy.sql.Sql 或 JDBC 进行连接池和准备好的语句

grails - 使用 "Getting Started with Grails"电子书进行集成测试时出现编译错误

spring - 在 Grails 3 中将 JNDI 添加到嵌入式 Tomcat 服务器

java - Groovy 中的泛型

gradle - 复制文件并重命名-Gradle

java - 如何使用Groovy脚本查找文件中是否存在特定字符串?

pdf - 使用ByteArrayOutputStream使用iText将水印添加到pdf

elasticsearch - 在 Elasticsearch 查询的内联脚本中使用聚合函数

java - Grails 线程

json - Groovy - 从字符串构造 json