【正文】
2021 Pearson AddisonWesley. All rights reserved. Sorting Arrays of Comparable 1324 Copyright 169。 2021 Pearson AddisonWesley. All rights reserved. GeneralizedSelectionSort class: interchange Method 1322 Copyright 169。 2021 Pearson AddisonWesley. All rights reserved. GeneralizedSelectionSort class: sort Method 1320 Copyright 169。 2021 Pearson AddisonWesley. All rights reserved. The Comparable Interface Semantics ? Other orderings may be considered, as long as they are a total ordering ? Such an ordering must satisfy the following rules: – (Irreflexivity) For no object o does o e before o – (Trichotomy) For any two object o1 and o2, one and only one of the following holds true: o1 es before o2, o1 es after o2, or o1 equals o2 – (Transitivity) If o1 es before o2 and o2 es before o3, then o1 es before o3 ? The equals of the pareTo method semantics should coincide with the equals method if possible, but this is not absolutely required 1318 Copyright 169。 2021 Pearson AddisonWesley. All rights reserved. The Comparable Interface Semantics ? The method pareTo must return – A negative number if the calling object es before the parameter other – A zero if the calling object equals the parameter other – A positive number if the calling object es after the parameter other ? If the parameter other is not of the same type as the class being defined, then a ClassCastException should be thrown 1316 Copyright 169。 ? It is the programmer39。 2021 Pearson AddisonWesley. All rights reserved. The Comparable Interface ? Chapter 6 discussed the Selection Sort algorithm, and examined a method for sorting a partially filled array of type double into increasing order ? This code could be modified to sort into decreasing order, or to sort integers or strings instead – Each of these methods would be essentially the same, but making each modification would be a nuisance – The only difference would be the types of values being sorted, and the definition of the ordering ? Using the Comparable interface could provide a single sorting method that covers all these cases 1314 Copyright 169。 2021 Pearson AddisonWesley. All rights reserved. Extending an Interface 1312 Copyright 169。 2021 Pearson AddisonWesley. All rights reserved. An Abstract Class Implementing an Interface 1310 Copyright 169。 2021 Pearson Ad