Fixed onjava/Range.java, added TestRange.java
This commit is contained in:
parent
cf109e7860
commit
68e98a3075
@ -5,7 +5,7 @@
|
||||
4: main
|
||||
5: main
|
||||
6: main
|
||||
6: ForkJoinPool.commonPool-worker-9
|
||||
7: main
|
||||
8: main
|
||||
8: ForkJoinPool.commonPool-worker-9
|
||||
9: main
|
||||
10: ForkJoinPool.commonPool-worker-5
|
||||
|
@ -32,7 +32,7 @@ public class Overloading {
|
||||
t.info();
|
||||
t.info("overloaded method");
|
||||
}
|
||||
// Overloaded constructor:
|
||||
// Calls overloaded constructor:
|
||||
new Tree();
|
||||
}
|
||||
}
|
||||
|
@ -12,12 +12,12 @@ class C2 implements I1, I2 {
|
||||
@Override
|
||||
public void f() {}
|
||||
@Override
|
||||
public int f(int i) { return 1; } // overloaded
|
||||
public int f(int i) { return 1; } // Overloaded
|
||||
}
|
||||
|
||||
class C3 extends C implements I2 {
|
||||
@Override
|
||||
public int f(int i) { return 1; } // overloaded
|
||||
public int f(int i) { return 1; } // Overloaded
|
||||
}
|
||||
|
||||
class C4 extends C implements I3 {
|
||||
|
@ -2,33 +2,29 @@
|
||||
// (c)2021 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Array creation methods that can be used without
|
||||
// qualifiers, using static imports:
|
||||
// Create arrays initialized with integer values.
|
||||
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 sequence [start..end) incrementing by step
|
||||
public static
|
||||
int[] range(int start, int end, int step) {
|
||||
int sz = (end - start)/step;
|
||||
if (step == 0)
|
||||
throw new
|
||||
IllegalArgumentException("Step cannot be zero");
|
||||
int sz = Math.max(0, step >= 0 ?
|
||||
(end + step - 1 - start) / step
|
||||
: (end + step + 1 - start) / step);
|
||||
int[] result = new int[sz];
|
||||
for(int i = 0; i < sz; i++)
|
||||
result[i] = start + (i * step);
|
||||
return result;
|
||||
} // Produce a sequence [start..end)
|
||||
public static int[] range(int start, int end) {
|
||||
return range(start, end, 1);
|
||||
}
|
||||
// Produce a sequence [0..n)
|
||||
public static int[] range(int n) {
|
||||
return range(0, n);
|
||||
}
|
||||
}
|
||||
|
29
onjava/TestRange.java
Normal file
29
onjava/TestRange.java
Normal file
@ -0,0 +1,29 @@
|
||||
// onjava/TestRange.java
|
||||
// (c)2021 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Basic test of Range.java
|
||||
import static onjava.Range.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TestRange {
|
||||
private static void show(int[] rng) {
|
||||
System.out.println(Arrays.toString(rng));
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
show(range(10, 21, 3));
|
||||
show(range(21, 10, -3));
|
||||
show(range(-5, 5, -3));
|
||||
show(range(-5, 5, 3));
|
||||
show(range(10, 21));
|
||||
show(range(10));
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
[10, 13, 16, 19]
|
||||
[21, 18, 15, 12]
|
||||
[]
|
||||
[-5, -2, 1, 4]
|
||||
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
*/
|
Loading…
x
Reference in New Issue
Block a user