很菜的问题,麻烦大家解答。 有一个学生类,属性是 名字,学号,分数在学生管理类(主要是对学生类进行添加,删除,修改,显示,排序等功能)里面新建了一个链表,并把一个个的学生添加到链表中问题是,怎么能按分数对学生进行排序?麻烦大家指教。本人是才上路的,谢谢了。在线等。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 哦,对了,我用的LinkedList,不知道是不是用Vector方便些?还望大家多多指教 在学生类里实现Comparable接口重写CompareTo(Objcet o)方法,里面比较就是用学生类的分数大小来返回不同的int值,这样学生管理类可以通过学生类调用CompareTo方法与另一个学生类比较来排序了。 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(); }} 冒泡,希尔排序!自己写算法也行, 或者将分数放到数组里面 用java.util.Arrays的sort()方法,可以进行排序,,, 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; } } 最好用TreeSet,可以根据CompareTo自动排序 继承的一点疑问,javap分析字节码 紧急求助! 关于jd-gui 和jad.exe反编出来的src差异巨大的问题! 用java做服务器,android客户端,socket就是连接不上 Socket连接问题 关于List与Set, 各自相对于对方的优缺点是什么呢? 请问super关键字是不是引用? 新手问题2,类和构造函数区别? 数据库进行双机切换的问题,紧急请教!!! 哪里有linux下的jdk可以下载? rs.getString()的问题 JAVA 入门问题 100分求!怎样用JAVA写一个浏览网页的应用呢?
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();
}
}
或者将分数放到数组里面
用java.util.Arrays的sort()方法,可以进行排序,,,
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;
}
}