@@ -2263,6 +2263,219 @@ public class StreamFromArray {
22632263< ! -- Sorting Arrays -- >
22642264## 数组排序
22652265
2266+ 根据对象的实际类型执行比较排序。一种方法是为不同的类型编写对应的排序方法,但是这样的代码不能复用。
2267+
2268+ 编程设计的一个主要目标是“将易变的元素与稳定的元素分开”,在这里,保持不变的代码是一般的排序算法,但是变化的是对象的比较方式。因此,使用策略设计模式而不是将比较代码放入许多不同的排序源码中。使用策略模式时,变化的代码部分被封装在一个单独的类(策略对象)中。
2269+
2270+ 您将一个策略对象交给相同的代码,该代码使用策略模式来实现其算法。通过这种方式,您将使用相同的排序代码,使不同的对象表达不同的比较方式。
2271+
2272+ Java 有两种方式提供比较功能。第一种方法是通过实现 ** java.lang. Comparable ** 接口的原生方法。这是一个简单的接口,只含有一个方法 ** compareTo()** 。该方法接受另一个与参数类型相同的对象作为参数,如果当前对象小于参数,则产生一个负值;如果参数相等,则产生零值;如果当前对象大于参数,则产生一个正值。
2273+
2274+ 这里有一个类,它实现了 ** Comparable ** 接口并演示了可比性,而且使用Java 标准库方法 ** Arrays . sort()** :
2275+
2276+ ```JAVA
2277+ // arrays/CompType.java
2278+ // Implementing Comparable in a class
2279+
2280+ import onjava. * ;
2281+
2282+ import java.util. Arrays ;
2283+ import java.util. SplittableRandom ;
2284+
2285+ import static onjava. ArrayShow . * ;
2286+
2287+ public class CompType implements Comparable<CompType > {
2288+ private static int count = 1 ;
2289+ private static SplittableRandom r = new SplittableRandom (47 );
2290+ int i;
2291+ int j;
2292+
2293+ public CompType (int n1 , int n2 ) {
2294+ i = n1;
2295+ j = n2;
2296+ }
2297+
2298+ public static CompType get () {
2299+ return new CompType (r. nextInt(100 ), r. nextInt(100 ));
2300+ }
2301+
2302+ public static void main (String [] args ) {
2303+ CompType [] a = new CompType [12 ];
2304+ Arrays . setAll(a, n - > get());
2305+ show(" Before sorting" , a);
2306+ Arrays . sort(a);
2307+ show(" After sorting" , a);
2308+ }
2309+
2310+ @Override
2311+ public String toString () {
2312+ String result = " [i = " + i + " , j = " + j + " ]" ;
2313+ if (count++ % 3 == 0 ) result += " \n " ;
2314+ return result;
2315+ }
2316+
2317+ @Override
2318+ public int compareTo (CompType rv ) {
2319+ return (i < rv. i ? - 1 : (i == rv. i ? 0 : 1 ));
2320+ }
2321+ }
2322+ /* Output:
2323+ Before sorting: [[i = 35, j = 37], [i = 41, j = 20], [i = 77, j = 79] ,
2324+ [i = 56, j = 68], [i = 48, j = 93],
2325+ [i = 70, j = 7] , [i = 0, j = 25],
2326+ [i = 62, j = 34], [i = 50, j = 82] ,
2327+ [i = 31, j = 67], [i = 66, j = 54],
2328+ [i = 21, j = 6] ]
2329+ After sorting: [[i = 0, j = 25], [i = 21, j = 6], [i = 31, j = 67] ,
2330+ [i = 35, j = 37], [i = 41, j = 20], [i = 48, j = 93] ,
2331+ [i = 50, j = 82], [i = 56, j = 68], [i = 62, j = 34] ,
2332+ [i = 66, j = 54], [i = 70, j = 7], [i = 77, j = 79] ]
2333+ */
2334+ ```
2335+
2336+ 当您定义比较方法时,您有责任决定将一个对象与另一个对象进行比较意味着什么。这里,在比较中只使用i值和j值
2337+ 将被忽略。
2338+
2339+ ** get()** 方法通过使用随机值初始化CompType 对象来构建它们。在 ** main()** 中,** get()** 与 ** Arrays . setAll()** 一起使用,以填充一个 ** CompType 类型** 数组,然后对其排序。如果没有实现 ** Comparable 接口** ,那么当您试图调用 ** sort()** 时,您将在运行时获得一个 ** ClassCastException ** 。这是因为 ** sort()** 将其参数转换为 ** Comparable 类型** 。
2340+
2341+ 现在假设有人给了你一个没有实现 ** Comparable 接口** 的类,或者给了你一个实现 ** Comparable 接口** 的类,但是你不喜欢它的工作方式而愿意有一个不同的对于此类型的比较方法。为了解决这个问题,创建一个实现 ** Comparator ** 接口的单独的类(在集合一章中简要介绍)。它有两个方法,** compare()** 和 ** equals()** 。但是,除了特殊的性能需求外,您不需要实现 ** equals()** ,因为无论何时创建一个类,它都是隐式地继承自 ** Object ** ,** Object ** 有一个equals()。您可以只使用默认的 ** Object equals()** 来满足接口的规范。
2342+
2343+ 集合类(注意复数;我们将在下一章节讨论它) 包含一个方法 ** reverseOrder()** ,它生成一个来 ** Comparator ** (比较器)反转自然排序顺序。这可以应用到比较对象:
2344+
2345+ ```JAVA
2346+ // arrays/Reverse.java
2347+ // The Collections.reverseOrder() Comparator
2348+
2349+ import onjava. * ;
2350+
2351+ import java.util. Arrays ;
2352+ import java.util. Collections ;
2353+
2354+ import static onjava. ArrayShow . * ;
2355+
2356+ public class Reverse {
2357+ public static void main (String [] args ) {
2358+ CompType [] a = new CompType [12 ];
2359+ Arrays . setAll(a, n - > CompType . get());
2360+ show(" Before sorting" , a);
2361+ Arrays . sort(a, Collections . reverseOrder());
2362+ show(" After sorting" , a);
2363+ }
2364+ }
2365+ /* Output:
2366+ Before sorting: [[i = 35, j = 37], [i = 41, j = 20],
2367+ [i = 77, j = 79] , [i = 56, j = 68],
2368+ [i = 48, j = 93], [i = 70, j = 7],
2369+ [i = 0, j = 25], [i = 62, j = 34],
2370+ [i = 50, j = 82] , [i = 31, j = 67],
2371+ [i = 66, j = 54], [i = 21, j = 6] ]
2372+ After sorting: [[i = 77, j = 79], [i = 70, j = 7],
2373+ [i = 66, j = 54] , [i = 62, j = 34],
2374+ [i = 56, j = 68], [i = 50, j = 82] ,
2375+ [i = 48, j = 93], [i = 41, j = 20],
2376+ [i = 35, j = 37] , [i = 31, j = 67],
2377+ [i = 21, j = 6], [i = 0, j = 25] ]
2378+ */
2379+ ```
2380+
2381+ 您还可以编写自己的比较器。这个比较CompType 对象基于它们的j值而不是它们的i值:
2382+
2383+ ```JAVA
2384+ // arrays/ComparatorTest.java
2385+ // Implementing a Comparator for a class
2386+
2387+ import onjava. * ;
2388+
2389+ import java.util. Arrays ;
2390+ import java.util. Comparator ;
2391+
2392+ import static onjava. ArrayShow . * ;
2393+
2394+ class CompTypeComparator implements Comparator<CompType > {
2395+ public int compare (CompType o1 , CompType o2 ) {
2396+ return (o1. j < o2. j ? - 1 : (o1. j == o2. j ? 0 : 1 ));
2397+ }
2398+ }
2399+
2400+ public class ComparatorTest {
2401+ public static void main (String [] args ) {
2402+ CompType [] a = new CompType [12 ];
2403+ Arrays . setAll(a, n - > CompType . get());
2404+ show(" Before sorting" , a);
2405+ Arrays . sort(a, new CompTypeComparator ());
2406+ show(" After sorting" , a);
2407+ }
2408+ }
2409+ /* Output:
2410+ Before sorting:[[i = 35, j = 37], [i = 41, j = 20], [i = 77, j = 79] ,
2411+ [i = 56, j = 68], [i = 48, j = 93], [i = 70, j = 7] ,
2412+ [i = 0, j = 25], [i = 62, j = 34], [i = 50, j = 82],
2413+ [i = 31, j = 67], [i = 66, j = 54], [i = 21, j = 6] ]
2414+ After sorting: [[i = 21, j = 6], [i = 70, j = 7], [i = 41, j = 20] ,
2415+ [i = 0, j = 25], [i = 62, j = 34], [i = 35, j = 37] ,
2416+ [i = 66, j = 54], [i = 31, j = 67], [i = 56, j = 68] ,
2417+ [i = 77, j = 79], [i = 50, j = 82], [i = 48, j = 93] ]
2418+ */
2419+ ```
2420+
2421+ < ! -- Using Arrays . sort() -- >
2422+ ## Arrays . sort()的使用
2423+
2424+ 使用内置的排序方法,您可以对实现了 ** Comparable ** 接口或具有 ** Comparator ** 的任何对象数组 或 任何原生数组进行排序。这里我们生成一个随机字符串对象数组并对其排序:
2425+
2426+ ```JAVA
2427+ // arrays/StringSorting.java
2428+ // Sorting an array of Strings
2429+
2430+ import onjava. * ;
2431+
2432+ import java.util. Arrays ;
2433+ import java.util. Collections ;
2434+
2435+ import static onjava. ArrayShow . * ;
2436+
2437+ public class StringSorting {
2438+ public static void main (String [] args ) {
2439+ String [] sa = new Rand .String (). array(20 );
2440+ show(" Before sort" , sa);
2441+ Arrays . sort(sa);
2442+ show(" After sort" , sa);
2443+ Arrays . sort(sa, Collections . reverseOrder());
2444+ show(" Reverse sort" , sa);
2445+ Arrays . sort(sa, String . CASE_INSENSITIVE_ORDER );
2446+ show(" Case-insensitive sort" , sa);
2447+ }
2448+ }
2449+ /* Output:
2450+ Before sort: [btpenpc, cuxszgv, gmeinne, eloztdv, ewcippc,
2451+ ygpoalk, ljlbynx, taprwxz, bhmupju, cjwzmmr,
2452+ anmkkyh, fcjpthl, skddcat, jbvlgwc, mvducuj,
2453+ ydpulcq, zehpfmm, zrxmclh, qgekgly, hyoubzl]
2454+
2455+ After sort: [anmkkyh, bhmupju, btpenpc, cjwzmmr, cuxszgv,
2456+ eloztdv, ewcippc, fcjpthl, gmeinne, hyoubzl,
2457+ jbvlgwc, ljlbynx, mvducuj, qgekgly, skddcat,
2458+ taprwxz, ydpulcq, ygpoalk, zehpfmm, zrxmclh]
2459+
2460+ Reverse sort: [zrxmclh, zehpfmm, ygpoalk, ydpulcq,taprwxz,
2461+ skddcat, qgekgly, mvducuj, ljlbynx, jbvlgwc,
2462+ hyoubzl, gmeinne, fcjpthl, ewcippc, eloztdv,
2463+ cuxszgv, cjwzmmr, btpenpc, bhmupju, anmkkyh]
2464+
2465+ Case-insensitive sort: [anmkkyh, bhmupju, btpenpc, cjwzmmr,
2466+ cuxszgv, eloztdv, ewcippc, fcjpthl, gmeinne,
2467+ hyoubzl, jbvlgwc, ljlbynx, mvducuj, qgekgly,
2468+ skddcat, taprwxz, ydpulcq, ygpoalk, zehpfmm, zrxmclh]
2469+ */
2470+ ```
2471+
2472+ 注意字符串排序算法中的输出。它是字典式的,所以它把所有以大写字母开头的单词放在前面,然后是所有以小写字母开头的单词。(电话簿通常是这样分类的。)无论大小写,要将单词组合在一起,请使用 ** String . CASE_INSENSITIVE_ORDER** ,如对sort()的最后一次调用所示。
2473+
2474+ Java 标准库中使用的排序算法被设计为最适合您正在排序的类型---- 原生类型的快速排序和对象的归并排序。
2475+
2476+
2477+ < ! -- Sorting in Parallel -- >
2478+ ## 并行排序
22662479
22672480< ! -- Searching with Arrays . binarySearch() -- >
22682481## binarySearch二分查找
0 commit comments