Java:Arrays.sort方法

For an array we do not want to write long sort code, so there is a good Arrays class provides a method --Arrays.sort method.

Arrays.sort method

Arrays.sort(array);
Arrays.sort(array, from_index, to_index);

The method is easily understood that the first method is performed directly on the array from small to large array quick sort, while the second method is only a predetermined range, i.e., array of array array [from_index] ~ array [to_index - 1] sort, keep in mind here is not included array [to_index] the number of yo.

Another point I almost forgot to say, here too early Arrays import categories, namely, to write the following code at the beginning

import java.utl.Arrays;

Code

package base;
import java.util.Arrays;

public class Arrays_sort
{
    public static void main(String[] args)
    {
        int a[] = {4, 3, 6, 5, 1, 2};
        Arrays.sort(a);
        for (int i = 0; i <= 5; i++)
            System.out.print(a[i] + " ");
        System.out.println();
        
        a = new int[]{4, 3, 6, 5, 1, 2};
        Arrays.sort(a, 2, 5 + 1);
        for (int i = 0; i <= 5; i++)
            System.out.print(a[i] + " ");
        System.out.println();
    }
}

Guess you like

Origin www.cnblogs.com/000zwx000/p/12461326.html