package practiceofcsp;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
import java.util.Scanner;
public class UseExampleForArrays {
public static void main(String[] args) {
int m,n;
System.out.println("please enter m and n");
Scanner reader=new Scanner(System.in);
m=reader.nextInt();
n=reader.nextInt();
Random r=new Random(m);
Student1[] s1=new Student1[n];
for(int i=0;i<n;i++) {
s1[i]=new Student1();
s1[i].id=i+1;
s1[i].grad1=r.nextInt(101);
s1[i].grad2=r.nextInt(101);
s1[i].grad3=r.nextInt(101);
s1[i].sum=s1[i].grad1+s1[i].grad2+s1[i].grad3;
}
Arrays.sort(s1);
System.out.println("Comparable接口实现的排序:");
for(int i=0;i<s1.length;i++) {
System.out.printf("%5d%5d%5d%5d%5d\n",s1[i].id,s1[i].grad1,s1[i].grad2,s1[i].grad3,s1[i].sum);
}
Student2[] s=new Student2[n];
for(int i = 0; i < n; i++){
s[i] = new Student2();
s[i].id = i+1;
s[i].grad1 = r.nextInt(101);
s[i].grad2 = r.nextInt(101);
s[i].grad3 = r.nextInt(101);
s[i].sum = s[i].grad1+s[i].grad2+s[i].grad3;
}
Arrays.sort(s, new Student2());
System.out.println("Comparator接口实现的排序:");
for (int i = 0; i < s.length; i++){
System.out.printf("%5d%5d%5d%5d%5d\n",s[i].id,s[i].grad1,s[i].grad2,s[i].grad3,s[i].sum);
}
}
}
class Student1 implements Comparable<Student1>{
public int id,grad1,grad2,grad3,sum;
@Override
public int compareTo(Student1 o) {
// TODO Auto-generated method stub
if(this.sum>o.sum) {
return 1;
}
else if(this.sum<o.sum) {
return -1;
}
else {
return 0;
}
}
}
class Student2 implements Comparator<Student2>{
public int id,grad1,grad2,grad3,sum;
@Override
public int compare(Student2 o1, Student2 o2) {
// TODO Auto-generated method stub
return o1.sum-o2.sum;
}
}