Groovy&Grails-技术问答-集合数据类型Ranges

定义一个范围


def ranges = start..end

创建一个ranges


def range = 1..10
assert range == [1,2,3,4,5,6,7,8,9,10]

range = 'a'..'c'
assert range == ['a','b','c']

range = 1..<8
assert range == [1,2,3,4,5,6,7]

遍历ranges


(1..5).each{
    println it
}

使用ranges创建一个list


assert [*1..4] == [1,2,3,4]
assert [1,*2..4] == [1,2,3,4]

你可能感兴趣的:(Groovy&Grails-技术问答-集合数据类型Ranges)