In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail about the Java collection framework and array sorting is what, the content of the article is of high quality, so the editor to share with you to do a reference, I hope you have a certain understanding of the relevant knowledge after reading this article.
By convention, when programming with java, you should use existing class libraries as much as possible. Of course, you can write a sorting method or framework yourself, but how many people can write better than those in JDK? Another benefit of using existing classes is that the code is easy to read and maintain, mainly about how to use existing class libraries to sort arrays and various Collection containers
The first thing to know is that two classes: java.util.Arrays and java.util.Collections (note the distinction with Collection) Collection is the top-level interface of the collection framework, while Collections contains many static methods. We use Arrays to sort arrays and Collections to sort associative framework containers, such as ArraysList,LinkedList, and so on.
Sort the array
For example, there is an integer array:
Int [] intArray = new int [] {4,1,3,-23}
How do we sort it? Are you thinking about the quick sorting algorithm at this time? Take a look at the following implementation:
Import java.util.*; public class Sort {public static void main (String [] args) {int [] intArray = new int [] {4,1,3,-23}; Arrays.sort (intArray);}}
In this way, we use Arrays's static method sort () to sort intArray in ascending order, and now the array has become {- 23 ~ (th).
If it is a character array:
String [] strArray = new String [] {"z", "a", "C"}
We use:
Arrays.sort (strArray)
The result of the sort is {C _ 4e _ r _ z}, and sort () sorts in ascending order according to the natural order of the elements. If you want to be case-insensitive, you can write:
Arrays.sort (strArray, String.CASE_INSENSITIVE_ORDER)
Of course, we can also specify a certain segment of the array to sort, for example, we want to sort the parts of the table 0-2 of the array (assuming the length of the array is greater than 3), and the rest remains the same, we can use:
Arrays.sort (strArray,0,2)
In this way, we sort only the first three elements, without affecting the later parts.
Of course, some people will wonder, how do I sort in descending order? One of the many sort methods
Sort (T [] a, Comparator
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.