java利用list对元素进行快速排序,并赋予排序等级
1.java的构造函数是在new一个对象时自动调用的函数,可以传参也可以不传参。
2.list集合不可以直接打印。
package package1;
public class Student {
String name;
String subject;
Integer score;
Integer order;
public Student(String name, String subject, int i, int j) {
// TODO Auto-generated constructor stub//给对象的属性赋值
this.name=name;
this.subject=subject;
this.score=i;//
this.order=j;
}
public void Student1(String string, String string2, String string3, String string4) {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public void setOrder(Integer order) {
this.order = order;
}
}
package package1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class listSort {
public static void main(String [] arugs){
List <Student>list1 =new ArrayList<Student>();
list1.add(new Student("李四","chinese",88,0));
list1.add(new Student("张三","chinese",67,0));
list1.add(new Student("王五","chinese",98,0));
list1.add(new Student("钱六","chinese",57,0));
Collections.sort(list1,new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if(o1.getScore()<o2.getScore()){
return -1;
}
if(o1.getScore()==o2.getScore())
return 0;
return 1;
}
});
for(int i=0;i<list1.size();i++){
list1.get(i).setOrder(i);
System.out.println(list1.get(i).score);
System.out.println(list1.get(i).order);
}
}
}
-------------------------------------------------------------------------
打印内容如下
98
0
88
1
67
2
57
3