| 8 | public class Range { |
| 9 | // Produce sequence [start..end) incrementing by step |
| 10 | public static |
| 11 | int[] range(int start, int end, int step) { |
| 12 | if (step == 0) |
| 13 | throw new |
| 14 | IllegalArgumentException("Step cannot be zero"); |
| 15 | int sz = Math.max(0, step >= 0 ? |
| 16 | (end + step - 1 - start) / step |
| 17 | : (end + step + 1 - start) / step); |
| 18 | int[] result = new int[sz]; |
| 19 | for(int i = 0; i < sz; i++) |
| 20 | result[i] = start + (i * step); |
| 21 | return result; |
| 22 | } // Produce a sequence [start..end) |
| 23 | public static int[] range(int start, int end) { |
| 24 | return range(start, end, 1); |
| 25 | } |