m0_ sixty-four million three hundred and eighty-four thousand t 2022-01-26 15:00:08 阅读数:925
Arrays.sort(arr); // Sort the array in ascending order
for(int i=0;i<arr.length;i++) { // The output array
System.out.print(arr[i]+" ");
}
}
}
Output:0 1 2 3 4 5 6 7 8 9
2. Arrays.sort(T[] a, int fromIndex, int toIndex) Sort the array in ascending order within the specified range
import java.util.Ar
《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing
rays; // Import Java.util.Arrays class
public class Taxis {
public static void main(String[]args) {
int arr[]=new int[] {2,3,7,6,4,9,0,1,8,5}; // Declaration array
Arrays.sort(arr,arr.length/2,arr.length); // Sort the second half of the array
for(int i=0;i<arr.length;i++) { // The output array
System.out.print(arr[i]+" ");
}
}
}
Output:2 3 7 6 4 0 1 5 8 9
3. Arrays.sort(T[] a, Comparator<? super T> c) Sorts the specified array of objects according to the order raised by the specified comparator .
null
Use sort() The default method is to sort in ascending order , What should we do if we want to sort in descending order ? At this time, we need to realize Comparator Interface to customize the comparison method .
import java.util.Arrays;
import java.util.Comparator;
public static void main(String[]args) {
Integer arr[]=new Integer[] {2,3,7,6,4,9,0,1,8,5};
// By anonymous inner class Comparator Interface
Arrays.sort(arr, new Comparator() {
@Override
// This method is used to compare the size of two numbers , Sort the positive and negative of the returned results , If we use arr2 - arr1 Sort in descending order
public int compare(Integer arr1, Integer arr2) {
return arr2 - arr1;
}
});
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+" ");
}
}
Output: 9 8 7 6 5 4 3 2 1 0
Because we can customize the comparator , So the data we compare is not limited to the basic data types , For arrays 、 The same applies to all classes .
Compare the first number of two-dimensional arrays
import java.util.Arrays;
import java.util.Comparator;
public class Taxis {
public static void main(String[]args) {
int [][]arrs=new int[][] { // Create a 2D array
{2,3,7},
{6,4,9},
{1,8,5}
};
Arrays.sort(arrs, new Comparator<int[]>() { // Define comparison method : Compare the first number of each row of the array
public int compare(int[] arrs1, int[] arrs2) {
return arrs1[0] - arrs2[0];
}
});
for(int i=0;i<arrs.length;i++) { // Output results
for(int j=0;j<arrs[0].length;j++) {
System.out.print(arrs[i][j]+" ");
}
System.out.println();
}
}
}
Output:
1 8 5
2 3 7
6 4 9
Compare classes in a certain way :
import java.util.Arrays;
import java.util.Comparator;
class Circle{ // Customize Circle class
double r;
public Circle(double r) {
this.r=r;
}
public double area() {
return 3.14rr;
}
copyright:author[m0_ sixty-four million three hundred and eighty-four thousand t],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261500068630.html