zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【Groovy】循环控制 ( Java 语法循环 | 默认的 IntRange 构造函数 | 可设置翻转属性的 IntRange 构造函数 | 可设置是否包含 to 的构造函数 | 0..9 简写 )

JAVA控制属性循环 设置 to 是否 默认
2023-06-13 09:18:00 时间

文章目录

一、使用 Java 语法循环


在 Groovy 中 , 使用 Java 语法进行循环 :

        // Java 语法样式的循环
        println ""
        print "( 1 ) : "
        for (int j = 0; j <= 9; j++) {
            print j + " "
        }

打印结果 :

( 1 ) : 0 1 2 3 4 5 6 7 8 9 

二、使用 IntRange 循环


1、使用默认的 IntRange 构造函数

使用默认的 IntRange 实例对象控制循环 ;

构造函数 :

	/**
	* 创建一个新的非包容性<code>IntRange</code>。如果 from <code>大于
	* <code>to</code>,将创建一个反向范围,并将<code>from</code>和<code>to</code> 进行交换。
	* 
	* @param from 范围中的第一个数字开始。
	* @param to 范围内的最后一个数字。
	* 如果范围包含的值超过{@link Integer#MAX_VALUE},则@throws会引发IllegalArgumentException。
	*/
    public IntRange(int from, int to) 

循环示例代码 :

// Groovy 循环 , 0 ~ 9 进行循环
println ""
print "( 1 ) : "
for (i in new IntRange(0, 9)) {
    print i + " "
}

执行结果 :

( 1 ) : 0 1 2 3 4 5 6 7 8 9 

2、使用可设置翻转属性的 IntRange 构造函数

构造函数 :

    /**
     * Creates a new non-inclusive aware <code>IntRange</code>.
     *
     * @param from    the first value in the range.
     * @param to      the last value in the range.
     * @param reverse <code>true</code> if the range should count from
     *                <code>to</code> to <code>from</code>.
     * @throws IllegalArgumentException if <code>from</code> is greater than <code>to</code>.
     */
    protected IntRange(int from, int to, boolean reverse)

代码示例 :

        // Groovy 循环 , 0 ~ 9 进行循环
        println ""
        print "( 2 ) : "
        for (i in new IntRange(0, 9, false)) {
            print i + " "
        }

        // Groovy 循环 , 9 ~ 0 进行循环
        println ""
        print "( 3 ) : "
        for (i in new IntRange(0, 9, true)) {
            print i + " "
        }

执行结果 :

( 2 ) : 0 1 2 3 4 5 6 7 8 9 
( 3 ) : 9 8 7 6 5 4 3 2 1 0 

3、使用可设置是否包含 to 的 IntRange 构造函数

构造函数 :

    /**
     * Creates a new inclusive aware <code>IntRange</code>.
     *
     * @param from    the first value in the range.
     * @param to      the last value in the range.
     * @param inclusive <code>true</code> if the to value is included in the range.
     */
    public IntRange(boolean inclusive, int from, int to)

代码示例 :

        // Groovy 循环 , 0 ~ 9 进行循环 , 不包含最后一个 to 元素 , 即 9
        // 只能打印出 0 ~ 8 的数字
        println ""
        print "( 4 ) : "
        for (i in new IntRange(false, 0, 9)) {
            print i + " "
        }

        // Groovy 循环 , 0 ~ 9 进行循环 , 包含最后一个 to 元素 , 即 9
        // 只能打印出 0 ~ 9 的数字
        println ""
        print "( 5 ) : "
        for (i in new IntRange(true, 0, 9)) {
            print i + " "
        }

执行结果 :

( 4 ) : 0 1 2 3 4 5 6 7 8 
( 5 ) : 0 1 2 3 4 5 6 7 8 9 

三、使用 0…9 简化方式的 IntRange 实例对象


0…9 的描述 , 相当于 new IntRange(0, 9) , 二者是等价的 ;

代码示例 :

        // Groovy 循环 , 0 ~ 9 进行循环
        println ""
        print "( 6 ) : "
        for (i in 0..9) {
            print i + " "
        }

        // 其中 0..9 相当于 new IntRange(0, 9)
        def range = 0..9
        println ""
        print "0..9 type : "
        println range.class

执行结果 :

( 6 ) : 0 1 2 3 4 5 6 7 8 9 
0..9 type : class groovy.lang.IntRange

四、完整代码示例


完整代码示例 :

class Test {
    static void main(args) {

        // Java 语法样式的循环
        println ""
        print "( 0 ) : "
        for (int j = 0; j <= 9; j++) {
            print j + " "
        }

        // Groovy 循环 , 0 ~ 9 进行循环
        println ""
        print "( 1 ) : "
        for (i in new IntRange(0, 9)) {
            print i + " "
        }

        // Groovy 循环 , 0 ~ 9 进行循环
        println ""
        print "( 2 ) : "
        for (i in new IntRange(0, 9, false)) {
            print i + " "
        }

        // Groovy 循环 , 9 ~ 0 进行循环
        println ""
        print "( 3 ) : "
        for (i in new IntRange(0, 9, true)) {
            print i + " "
        }

        // Groovy 循环 , 0 ~ 9 进行循环 , 不包含最后一个 to 元素 , 即 9
        // 只能打印出 0 ~ 8 的数字
        println ""
        print "( 4 ) : "
        for (i in new IntRange(false, 0, 9)) {
            print i + " "
        }

        // Groovy 循环 , 0 ~ 9 进行循环 , 包含最后一个 to 元素 , 即 9
        // 只能打印出 0 ~ 9 的数字
        println ""
        print "( 5 ) : "
        for (i in new IntRange(true, 0, 9)) {
            print i + " "
        }


        // Groovy 循环 , 0 ~ 9 进行循环
        println ""
        print "( 6 ) : "
        for (i in 0..9) {
            print i + " "
        }

        // 其中 0..9 相当于 new IntRange(0, 9)
        def range = 0..9
        println ""
        print "0..9 type : "
        println range.class

        println ""
    }
}

执行结果 :

( 0 ) : 0 1 2 3 4 5 6 7 8 9 
( 1 ) : 0 1 2 3 4 5 6 7 8 9 
( 2 ) : 0 1 2 3 4 5 6 7 8 9 
( 3 ) : 9 8 7 6 5 4 3 2 1 0 
( 4 ) : 0 1 2 3 4 5 6 7 8 
( 5 ) : 0 1 2 3 4 5 6 7 8 9 
( 6 ) : 0 1 2 3 4 5 6 7 8 9 
0..9 type : class groovy.lang.IntRange