(String[] args)
| 6 | |
| 7 | public class Ranges { |
| 8 | public static void main(String[] args) { |
| 9 | // The traditional way: |
| 10 | int result = 0; |
| 11 | for(int i = 10; i < 20; i++) |
| 12 | result += i; |
| 13 | System.out.println(result); |
| 14 | |
| 15 | // for-in with a range: |
| 16 | result = 0; |
| 17 | for(int i : range(10, 20).toArray()) |
| 18 | result += i; |
| 19 | System.out.println(result); |
| 20 | |
| 21 | // Use streams: |
| 22 | System.out.println(range(10, 20).sum()); |
| 23 | } |
| 24 | } |
| 25 | /* Output: |
| 26 | 145 |