public class Student {
private String name;
public int Score;
public Student(){}
public Student(String name, int corse) {
super();
this.name = name;
this.Score = corse;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCorse() {
return Score;
}
public void setCorse(int corse) {
this.Score = corse;
}
public static void sortByScore(List<Student> list){//因为排序不属于莫个成员变量特有的属性所这里用了static 修饰符
Student s=null;
for (int i = 0; i < list.size()-1; i++) {
for (int j = list.size()-1; j >i; j--) {
if(list.get(j).Score>list.get(j-1).Score){
//交换的是整个Student对象
s=list.get(j);
list.set(j, list.get(j-1));
list.set(j-1, s);
}
}
}
}
public static void printout( ArrayList<Student>list){
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).name+" "+list.get(i).Score);
}
}
public static void main(String []args){
ArrayList<Student> list =new ArrayList <Student>() ;
Student stu=new Student("张三", 85);
list.add(stu);
int []a={4,7};
a.toString();
}
}[/code]结果[code=java]赵六 98
兰兰 96
李四 88
张三 85
王五 68[/code]一定要把整个Student对象交换作者: 匿名 时间: 2011-7-29 00:44 标题: 回复 楼主 的帖子 例如:[code]//对学生进行成绩排序
//应用Comparable 接口实现排序
import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private int age;
private double result;
public Student(String n,int a,double r)
{
name = n;
age = a;
result = r;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public double getResult()
{
return result;
}
public int compareTo(Student other)
{
if(result < other.result) return -1;
if(result > other.result) return 1;
return 0;
}
}
public class StudentSortTest
{
public static void main(String[] args)
{
Student[] staff = new Student[5];
staff[0] = new Student("wang",13,50.5);
staff[1] = new Student("wang",13,80.0);
staff[2] = new Student("wang",13,90.5);
staff[3] = new Student("wang",13,70.0);
staff[4] = new Student("wang",13,60.5);