35 lines
1010 B
Java
35 lines
1010 B
Java
// onjava/Range.java
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
|
// Array creation methods that can be used without
|
|
// qualifiers, using static imports:
|
|
package onjava;
|
|
|
|
public class Range {
|
|
// Produce a sequence [0..n)
|
|
public static int[] range(int n) {
|
|
int[] result = new int[n];
|
|
for(int i = 0; i < n; i++)
|
|
result[i] = i;
|
|
return result;
|
|
}
|
|
// Produce a sequence [start..end)
|
|
public static int[] range(int start, int end) {
|
|
int sz = end - start;
|
|
int[] result = new int[sz];
|
|
for(int i = 0; i < sz; i++)
|
|
result[i] = start + i;
|
|
return result;
|
|
}
|
|
// Produce a sequence [start..end) incrementing by step
|
|
public static
|
|
int[] range(int start, int end, int step) {
|
|
int sz = (end - start)/step;
|
|
int[] result = new int[sz];
|
|
for(int i = 0; i < sz; i++)
|
|
result[i] = start + (i * step);
|
|
return result;
|
|
}
|
|
}
|