Use arrays and collections to complete efficient sorting

xiaoxiao2021-03-06  41

If you are familiar with the data structure and algorithms, you can write an efficient sorting method yourself. But if you are not very familiar, JDK provides you with very convenient classes: java.util.arrays and java.util.collection. Among them, java.util.arrays is primarily processed, including the object array, find, comparison, sorting, etc. Java.util.Collection also has the same function, but it is handled by the object linked list. And their methods are static, can be called directly. The following is a specific example:

// Sort Object [] Objects = getObjectArray (); // to get the object array arrays.sort (Objects) you want to sort.

// Sort List ObjectList = getObjectList (); // Get the object linked list of the object you want to sort. Collectes.Sort (ObjectList);

Note: When using the above method, an object in an object array or an object linked list must be implemented: java.lang.comparable

With this method, you can complete a very efficient sorting method. For more detailed use of Arrays and Collectes, please refer to the JDK document. // One will be sorted in << Thinking in java >> to see a piece of code for a shot! It is called: The vector code will be sorted as follows:

import java.util *;. interface Compare {boolean lessThan (Object lhs, Object rhs); boolean lessThanOrEqual (Object lhs, Object rhs);} class SortVector extends Vector {private Compare compare; // To hold the callback public SortVector (Compare Comp) {compare = comp;} public void sort () {Quicksort (0, size () - 1);} // Quick Sort Private Void Quicksort (int LEFT, INT Right) {if (Right> LEFT) {Object O1 = Elementat (Right); INT i = left - 1; int J = Right; while (true) {while (Compare.lesthan (Elementat ( i), O1)); while (j> 0) IF (Compare). Lessthanorequal (Elementat (- j)) Break; // out of while if (i> = j) Break; swap (i, j);} swap (i, right); Quicksort (Left, i-1 ); Quicksort (I 1, Right);}} // Exchange position private void swap (int log1, int log2) {Object TMP = Elementat (LOC1); setElementat (Elementat (LOC2), LOC1); SETELEMENTAT (TMP, LOC2);}} public class stringsorttest { // Custom comparative rule static class stringcompare imports compare {public boolean lessthan (Object L, Object R) {return ((string) l) .tolowercase (). Compareto ((String) r) .tolowercase ()) <0 Public Boolean Lessthanorequal (Object L, Object R) {Return ((String) L) .tolowercase (). Compareto ((String) R) .tolowercase ()) <= 0;}} public static void main (String [] args) {SortVector SV = New SortVector (New StringCompare ()); // Add Element Sv.AddeElement ("D"); sv.addelement ("a"); sv.addelement ("c"); SV. Addelement ("c"); // Sort Sv.Sort (); Enumeration E = sv.elements ();

// Output result while (E.hasMoreElements ()) System.out.println (E.NEXTELEMENT ());}} code Detailed: Because we have to be sorted by Vector, Class SortVector Extends Vector. It is a unified larger interface.

Note Class StringCompare, define the rules of String comparison, how do we want to write an XXXXVector, just just write a class xxxxxcompare imports compare.

This code classically shows the usage of the interface, while inspiring how we plan the inheritance relationship.

转载请注明原文地址:https://www.9cbs.com/read-78452.html

New Post(0)