Several usage of Array. sort () in Java

Source: Internet
Author: User

**************************************** **************

* Recommended high-quality books:Java from getting started to Jing Tong

* This book is comprehensive and easy to understand. It is very suitable for beginners without any programming experience,

* It can also be used as a reference book for software developers and college teachers and students.

**************************************** **************

In Java, the Arrays class has an sort () method, which is a static method of the Arrays class and is very useful when sorting Arrays.

However, there are several sort () parameters. I will introduce them one by one.

========================================================== ====================

1. Arrays. sort (int [])

This form sorts all elements of an array in ascending order.

For example (click "+" to view the code ):

1 import java. util. arrays; 2 3 public class Main {4 public static void main (String [] args) {5 6 int [] a = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5}; 7 Arrays. sort (a); 8 for (int I = 0; I <. length; I ++) {9 System. out. print (a [I] + ""); 10} 11} 12 13}View Code

The running result is as follows:

0 1 2 3 4 5 6 7 8 9

---------------------------------------------------------

2. Arrays. sort (int [] a, int fromIndex, int toIndex)

This form is part of the array sorting, that is, the array a subscript from fromIndex to the toIndex-1 of the elements of the sorting, note: The subscript is toIndex elements do not participate in the sorting Oh!

For example (click "+" to view the code ):

1 import java. util. arrays; 2 3 public class Main {4 public static void main (String [] args) {5 6 int [] a = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5}; 7 Arrays. sort (a, 0, 3); 8 for (int I = 0; I <. length; I ++) {9 System. out. print (a [I] + ""); 10} 11} 12 13}View Code

The running result is as follows:

7 8 9 2 3 4 1 0 6 5

In the above example, we just sorted 9 8 7 into 7 8 9

----------------------------------------------------------

3. public static <T> void sort (T [] a, int fromIndex,Int toIndex,Comparator <? Super T> c)

There is a constraint above, that is, the order can only be small to large. If we want to be large to small, we need to use this method.

The generic type in Java is involved here. If you are not familiar with it, you can leave it alone for the moment. If you really want to know it, read the book I recommended above, which has a detailed introduction.

The reader only needs to understand the following example. In fact, there is only one more parameter of the Comparator type.

1 package test; 2 3 import java. util. arrays; 4 import java. util. comparator; 5 6 public class Main {7 public static void main (String [] args) {8 // note that you cannot use the basic type (int, double, char) 9 // and use their corresponding class 10 Integer [] a = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5}; 11 // define a custom class MyComparator object 12 Comparator cmp = new MyComparator (); 13 Arrays. sort (a, cmp); 14 for (int I = 0; I <. length; I ++) {15 System. out. print (a [I] + ""); 16} 17} 18} 19 // Comparator is an interface, so here we define the class MyComparator to implents this interface 20 // instead of extends Comparator21 class MyComparator implements Comparator <Integer> {22 @ Override23 public int compare (Integer o1, Integer o2) {24 // If n1 is less than n2, a positive value is returned. If n1 is greater than n2, a negative value is returned. 25 // This is reversed, 26 if (o1 <o2) {27 return 1; 28} else if (o1> o2) {29 return-1; 30} else {31 return 0; 32} 33} 34 35}View Code

The running result is as follows:

9 8 7 6 5 4 3 2 1 0

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.