The structures you’ve seen so far have evaluated a Boolean test once and changed the path of execution once based on the result of the condition. Looping, on the other hand, repeats the execution of a block of code multiple times. The loops available in Groovy are while and for , both of which we cover here.
Looping with while:
The while construct is like its Java counterpart. The only difference is the one you’ve seen already—the power of Groovy Boolean test expressions. To summarize very briefly, the Boolean test is evaluated, and if it’s true, the body of the loop is then executed. The test is then re-evaluated, and so forth. Only when the test becomes false does control proceed past the while loop. Listing 6.8 shows an example that removes all entries from a list.
- Listing 6.8 Example while loops
- def list = [1,2,3]
- while (list) {
- list.remove(0)
- }
- assert list == []
- while (list.size() < 3) list << list.size()+1
- assert list == [1,2,3]
Looping with for:
Considering it is probably the most commonly used type of loop, the for loop in Java is relatively hard to use, when you examine it closely. Through familiarity, people who have used a language with a similar structure (and there are many such languages) grow to find it easy to use, but that is solely due to frequent use, not due to good design. Although the nature of the traditional for loop is powerful, it is rarely used in a way that can’t be more simply expressed in terms of iterating through a collection-like data structure. Groovy embraces this simplicity, leading to probably the biggest difference in control structures between Java and Groovy.
Groovy for loops follow this structure:
where variable may optionally have a declared type. The Groovy for loop iterates over the iterable. Frequently used iterables are ranges, collections, maps, arrays, iterators, and enumerations. In fact, any object can be an iterable. Groovy applies the same logic as for object iteration, described in chapter 8.
Curly braces around the body are optional if it consists of only one statement. Listing 6.9 shows some of the possible combinations.
- Listing 6.9 Multiple for loop examples
- // 1) Typed, over string range, no braces
- def store = ''
- for (String i in 'a'..'c') store += i
- assert store == 'abc'
- // 2) Untyped, over list as collection, braces
- store = ''
- for (i in [1, 2, 3]) {
- store += i
- }
- assert store == '123'
- // 3) Untyped, over half-exclusive IntRange, braces
- def myString = 'Equivalent to Java'
- store = ''
- for (i in 0 ..< myString.size()) {
- store += myString[i]
- }
- assert store == myString
- // 4) Untyped, over string as collection, braces
- store = ''
- for (i in myString) {
- store += i
- }
- assert store == myString
You can use it to print a file line-by-line via
- def file = new File('myFileName.txt')
- for (line in file) println line
- def matcher = '12xy3'=~/\d/
- for (match in matcher) println match
- for (x in null) println 'This will not be printed!'
- for (x in new Object()) println "Printed once for object $x"
- for (x in 0..9) { println x }
- (0..9).each { println it }
Supplement:
* Groovy Document > Control Structures > Looping
沒有留言:
張貼留言