Not much nonsense said directly to the code first:
Import java.util.*;
Import java.io.*;
public class main{
static int [] DP = new int [1010];
public static void Main (String [] args) throws ioexception{
Mouse [] mice = new Mouse [1010];
FileReader fr=new FileReader ("In.txt"); Read the file
bufferedreader read = new BufferedReader (FR);
String str = "";
int n=1;
while ((str = read.readline ())!=null) {
String [] s= str.split ("");
Mice[n] = new Mouse (); Object instantiation, very important
mice[n].weight = Integer.parseint (S[0]);
Mice[n].speed =integer.parseint (s[1]);
n++;
}
SYSTEM.OUT.PRINTLN (n);
Arrays.sort (mice,1,n); Sort (int start,int end) includes the start index, excluding the end index for
(int i=1;i<n;i++) {
System.out.println (mice[i].weight+ "" +mice[i].speed);
}} Class Mouse implements comparable{ //Implements comparable interface
int weight;
int speed;
public int compareTo (Object o) { //rewrite CompareTo method
Mouse m= (Mouse) o;
Return weight>m.weight?1: (weight==m.weight?0:-1);
}
Another attached arrays.sort usage:
1. Number sort int[] Intarray = new int[] {4, 1, 3,-23};
Arrays.sort (Intarray);
Output: [-23, 1, 3, 4]
2. String sorting , first uppercase after lowercase string[] strarray = new string[] {"Z", "a", "C"};
Arrays.sort (Strarray);
Output: [C, A, z]
3. Strictly alphabetical order, that is, ignore the case of the sort case-insensitive sort
Arrays.sort (Strarray, String.case_insensitive_order);
Output: [A, C, z]
4. Reverse sorting , Reverse-order sort
Arrays.sort (Strarray, Collections.reverseorder ());
Output: [Z, A, C]
5. Ignore case Reverse sort case-insensitive reverse-order sort
Arrays.sort (Strarray, String.case_insensitive_order);
Collections.reverse (Arrays.aslist (Strarray));
Output: [Z, C, a]
6, Object array sorting
To sort an array of objects, you implement the Java.util.Comparator interface yourself
Example:
Common_user[] Userlisttemp=new common_user[temp.size ()];
Arrays.sort (Userlisttemp, New Pinyincomparator ());
Pinyincomparator implements the comparator interface and rewrites the Compare method to tell arrays what rules to compare the size of two objects.
The above mentioned is the entire content of this article, I hope you can enjoy.