Use arrays and collections to complete efficient sorting

xiaoxiao2021-03-06  40

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 the object array

Object [] Objects = getObjectArray (); // A number of objects you want to sort

Arrays.Sort (Objects);

/ / Sort the object linked list

List ObjectList (); // Get the object linked list you want to sort

Collects.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.

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

New Post(0)