MCPcopy Index your code
hub / github.com/BruceEckel/OnJava8-Examples / CompType

Class CompType

arrays/CompType.java:11–40  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9import static onjava.ArrayShow.*;
10
11public class CompType implements Comparable<CompType> {
12 int i;
13 int j;
14 private static int count = 1;
15 public CompType(int n1, int n2) {
16 i = n1;
17 j = n2;
18 }
19 @Override public String toString() {
20 String result = "[i = " + i + ", j = " + j + "]";
21 if(count++ % 3 == 0)
22 result += "\n";
23 return result;
24 }
25 @Override public int compareTo(CompType rv) {
26 return (i < rv.i ? -1 : (i == rv.i ? 0 : 1));
27 }
28 private static SplittableRandom r =
29 new SplittableRandom(47);
30 public static CompType get() {
31 return new CompType(r.nextInt(100), r.nextInt(100));
32 }
33 public static void main(String[] args) {
34 CompType[] a = new CompType[12];
35 Arrays.setAll(a, n -> get());
36 show("Before sorting", a);
37 Arrays.sort(a);
38 show("After sorting", a);
39 }
40}
41/* Output:
42Before sorting: [[i = 35, j = 37], [i = 41, j = 20], [i
43= 77, j = 79]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected