maa*_*aaz 20 arrays string groovy
我有一个这样的列表:
List tripIds    = new ArrayList()
def sql = Sql.newInstance("jdbc:mysql://localhost:3306/steer", "root",
            "", "com.mysql.jdbc.Driver")
        sql.eachRow("SELECT trip.id from trip JOIN department WHERE organization_id = trip.client_id AND  department.id =1") {
            println "Gromit likes ${it.id}"
            tripIds << it.id
        } 
现在印刷三脚架给我的价值
  [1,2,3,4,5,6,]
现在我想将此列表转换为简单的字符串
 1,2,3,4,5,6
我该怎么做
Dav*_*ton 42
使用join,例如,
tripIds.join(", ")
不相关,但是如果你只想创建一个来自另一个列表的东西的列表,你最好做一些像map或者collect手动创建一个列表并附加到它的东西,这不是惯用的,例如(未经测试),
def sql = Sql.newInstance("jdbc:mysql://localhost:3306/steer", "root", "", "com.mysql.jdbc.Driver")
def tripIds = sql.map { it.id }
或者,如果您只关心生成的字符串,
def tripIds = sql.map { it.id }.join(", ")
在groovy:
def myList = [1,2,3,4,5]
def asString = myList.join(", ")
使用Groovy添加到Collection 的join方法
List l = [1,2,3,4,5,6]
assert l.join(',') == "1,2,3,4,5,6"
| 归档时间: | 
 | 
| 查看次数: | 43528 次 | 
| 最近记录: |