在 Groovy - 在数组列表中如何用数字零填充版本中的空字段?

In Groovy -in the list of array How to fill empty fields in version with number zero?

在groovy中,如何在列表

版本的空字段中添加零
def list = [1.0,
1.9,
1.11.0,
1.6,
1.7,
1.7.1,
1.8]

预期输出

1.0.0,
1.9.0,
1.11.0,
1.6.0,
1.7.0,
1.7.1,
1.8.0

您在此处显示的代码无效 Groovy 代码,无法编译。您不能定义像 1.11.0 这样的数字。那必须是一个字符串。

以下为该特定数据输入生成所需的输出:

def list = ['1.0',
            '1.9',
            '1.11.0',
            '1.6',
            '1.7',
            '1.7.1',
            '1.8']

println list.collect {
    String output = it
    if(output.count('.') < 2) output += '.0'
    output
}.join(',\n')

也可以这样做:

def list = ['1.0',
            '1.9',
            '1.11.0',
            '1.6',
            '1.7',
            '1.7.1',
            '1.8']

println list.collect {
    if(it.count('.') < 2) it += '.0'
    it
}.join(',\n')

或者这样:

def list = ['1.0',
            '1.9',
            '1.11.0',
            '1.6',
            '1.7',
            '1.7.1',
            '1.8']

println list.collect {
    it.count('.') < 2 ? it += '.0' : it
}.join(',\n')