Java Reference
In-Depth Information
Display 13.6 Sorting Arrays of Comparable (part 1 of 2)
1 /**
2 Demonstrates sorting arrays for classes that
3 implement the Comparable interface.
4 */
5 public class ComparableDemo
6{
7
The classes Double and String do
implement the Comparable interface.
public static void main(String[] args)
8
{
9
Double[] d = new Double[10];
10
for (int i = 0; i < d.length; i++)
d[i] = new Double(d.length i);
11
12
System.out.println("Before sorting:");
13
int i;
14
for (i = 0; i < d.length; i++)
15
System.out.print(d[i].doubleValue() + ", ");
16
System.out.println();
17
GeneralizedSelectionSort.sort(d, d.length);
18
System.out.println("After sorting:");
19
for (i = 0; i < d.length; i++)
20
System.out.print(d[i].doubleValue() + ", ");
21
System.out.println();
22
String[] a = new String[10];
23
a[0] = "dog";
24
a[1] = "cat";
25
a[2] = "cornish game hen";
26
int numberUsed = 3;
27
System.out.println("Before sorting:");
28
for (i = 0; i < numberUsed; i++)
29
System.out.print(a[i] + ", ");
30
System.out.println();
31
32
GeneralizedSelectionSort.sort(a, numberUsed);
33
System.out.println("After sorting:");
34
for (i = 0; i < numberUsed; i++)
35
System.out.print(a[i] + ", ");
36
System.out.println();
37
}
38
}
(continued)
Search WWH ::




Custom Search