NAME:dapeng       AGE 27        CJ:100
NAME:jinlin       AGE 23        CJ:76
NAME:yaojian       AGE 28        CJ:82
NAME:dandan       AGE 25        CJ:93
NAME:pengcheng       AGE 23        CJ:79
    76    79    82    93    100我是刚开始学java的新手,上面是我的程序现在的打印结果,但是这个不是我需要的结果。
我需要打印后应该把“CJ”排好序后打印,类似如上面5行的形式。但是是按照"CJ"的“小-大”顺序排列的。代码如下:
import java.util.Arrays;public class ArrSort {                       //主执行类
public static void main(String[] args) { Student s1 = new Student("dapeng", 27, 100);
Student s2 = new Student("jinlin", 23, 76);
Student s3 = new Student("yaojian", 28, 82);
Student s4 = new Student("dandan", 25, 93);
Student s5 = new Student("pengcheng", 23, 79);

int ia[] = new int
[]{s1.getKscj() ,s2.getKscj() ,s3.getKscj() ,s4.getKscj() ,s5.getKscj()}; 
Arrays.sort(ia);
for(int i = 0; i <ia.length; i++){
System.out.print("    " + ia[i]);
}
}
}请大大们帮我改改,谢谢了~~~~

解决方案 »

  1.   

    Arrays.sort(ia);这个方法的使用,前提是你的Student实现了Comparable 接口,你实现了没有?
      

  2.   

    这个是student类:public class Student implements Comparable { public Student(String string, int i, int j) {
    this.name = string;
    this.age = i;
    this.kscj = j;
    }
    private String name;
    private int age;
    private int kscj;
    public int compareTo(Object o) {
    if (this.kscj > ((Student) o).kscj) {
    return 1;
    } else {
    return -1;
    }
    }
    public String toString() {
    return "NAME:" + this.name + "AGE" + this.age + "CJ:" + this.kscj;
    }}
    下面是测试类:
    import java.util.Arrays;
    public class ArrSort { // 主执行类
    public static void main(String[] args) {
    Student s1 = new Student("dapeng", 27, 100);
    Student s2 = new Student("jinlin", 23, 76);
    Student s3 = new Student("yaojian", 28, 82);
    Student s4 = new Student("dandan", 25, 93);
    Student s5 = new Student("pengcheng", 23, 79);
    Student[] student = new Student[5];
    student[0] = s1;
    student[1] = s2;
    student[2] = s3;
    student[3] = s4;
    student[4] = s5;
    Arrays.sort(student);
    for (int i = 0; i < student.length; i++) {
    System.out.println(student[i]);
    }
    }
    }
      

  3.   

    public static void main(String[] args) { Student s1 = new Student("dapeng", 27, 100);
    Student s2 = new Student("jinlin", 23, 76);
    Student s3 = new Student("yaojian", 28, 82);
    Student s4 = new Student("dandan", 25, 93);
    Student s5 = new Student("pengcheng", 23, 79);
    Student sa[] = new Student[]{s1,s2,s3,s4,s5};

    int ia[] = new int
    []{s1.getKscj() ,s2.getKscj() ,s3.getKscj() ,s4.getKscj() ,s5.getKscj()};  
    Arrays.sort(ia); 

    for(int i = 0; i <ia.length; i++){
    for(int j = 0; j <sa.length; j++){
    if (ia[i] == sa[j].getKscj()) { System.out.print("NAME:" + sa[j].getName());
    System.out.print(" AGE:" + sa[j].getAge());
    System.out.print(" CJ:" + sa[j].getKscj());
    System.out.println();
    }
    }

    }  }