有一个学生类,属性是 名字,学号,分数在学生管理类(主要是对学生类进行添加,删除,修改,显示,排序等功能)里面新建了一个链表,并把一个个的学生添加到链表中问题是,怎么能按分数对学生进行排序?麻烦大家指教。本人是才上路的,谢谢了。在线等。

解决方案 »

  1.   

    哦,对了,我用的LinkedList,不知道是不是用Vector方便些?还望大家多多指教
      

  2.   

       在学生类里实现Comparable接口重写CompareTo(Objcet o)方法,里面比较就是用学生类的分数大小来返回不同的int值,这样学生管理类可以通过学生类调用CompareTo方法与另一个学生类比较来排序了。
      

  3.   


    package com.qq.server;import java.util.Arrays;
    import java.util.LinkedList;public class Test2 {
    public static void main(String[] args) {
    LinkedList<Student> list = new LinkedList<Student>();

    list.add(new Student(1,123));
    list.add(new Student(2,113));
    list.add(new Student(3,23));
    list.add(new Student(4,84));
    list.add(new Student(5,423));
    list.add(new Student(6,33));
    list.add(new Student(7,67));

    Student[] tempArray = list.toArray(new Student[] {});
    Arrays.sort(tempArray); for (int i = 0; i < tempArray.length; i++) {
    System.out.println(tempArray[i].toString());
    }
    }
    }class Student implements Comparable {
    private Integer studentId; private double source; public double getSource() {
    return source;
    } public void setSource(double source) {
    this.source = source;
    } public Integer getStudentId() {
    return studentId;
    } public void setStudentId(Integer studentId) {
    this.studentId = studentId;
    } public Student() { } public Student(Integer studentId, double source) {
    this.setStudentId(studentId);
    this.setSource(source);
    } public int compareTo(Object stu2) {
    int returnInt = 0;
    if (this.getSource() - ((Student) stu2).getSource() > 0) {
    returnInt = 1;
    } else if (this.getSource() - ((Student) stu2).getSource() == 0) {
    returnInt = 0;
    } else {
    returnInt = -1;
    }
    return returnInt;
    } public String toString() {
    return this.getStudentId() + " --> " + this.getSource();
    }
    }
      

  4.   

           冒泡,希尔排序!自己写算法也行,
        或者将分数放到数组里面
     用java.util.Arrays的sort()方法,可以进行排序,,,
      

  5.   


    package test;import java.util.Arrays;public class MyTest {

    public static void main(String[] args) {
    Student s [] = new Student [4];
    s[0] = new Student("aa","001",98);
    s[1] = new Student("bb","002",58);
    s[2] = new Student("cc","003",78);
    s[3] = new Student("dd","004",38);
    Arrays.sort(s);
    for(int i = 0; i<s.length; i++){
    System.out.println(s[i]);
    }
    }
    }
    class Student implements Comparable<Student>{
    private String name;
    private String no;
    private int score;
    public Student(String name,String no,int score){
    this.name = name;
    this.no = no;
    this.score = score;
    }
    public int compareTo(Student o) {
    // TODO Auto-generated method stub
    if(this.score > o.score){
    return 1;
    }else if(this.score < o.score){
    return -1;
    }else{
    return 0;
    }
    }
    public String toString(){
    return "name:"+name+",no:"+no+",score:"+score;
    }

    }
      

  6.   

    最好用TreeSet,可以根据CompareTo自动排序