【Groovy】循环控制 ( Java 语法循环 | 默认的 IntRange 构造函数 | 可设置翻转属性的 IntRange 构造函数 | 可设置是否包含 to 的构造函数 | 0..9 简写 )
生活随笔
收集整理的這篇文章主要介紹了
【Groovy】循环控制 ( Java 语法循环 | 默认的 IntRange 构造函数 | 可设置翻转属性的 IntRange 构造函数 | 可设置是否包含 to 的构造函数 | 0..9 简写 )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、使用 Java 語法循環
- 二、使用 IntRange 循環
- 1、使用默認的 IntRange 構造函數
- 2、使用可設置翻轉屬性的 IntRange 構造函數
- 3、使用可設置是否包含 to 的 IntRange 構造函數
- 三、使用 0..9 簡化方式的 IntRange 實例對象
- 四、完整代碼示例
一、使用 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 92、使用可設置翻轉屬性的 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 03、使用可設置是否包含 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..9println ""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..9println ""print "0..9 type : "println range.classprintln ""} }執行結果 :
( 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 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的【Groovy】循环控制 ( Java 语法循环 | 默认的 IntRange 构造函数 | 可设置翻转属性的 IntRange 构造函数 | 可设置是否包含 to 的构造函数 | 0..9 简写 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Groovy】字符串 ( 字符串注入函
- 下一篇: 【Groovy】循环控制 ( Numbe