Vector 排序问题 Vector 中的元素如何进行排序呢?? 是有方法,还是需要自己写呢?? 我没有找到请高手指教! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先为Vector中的元素实现Comparator接口,实现compareTo和equals方法,然后用java.util.Arrays类的sort方法实现排序Vector v;//....java.util.Arrays.sort(v.toArray(),comparator); 下面是一个学生名字和成绩输入并排序的例子import java.util.*;import java.io.*;public class Student implements Comparable{ private String name =""; private double score = 0; public Student (String name, double score){ this.name = name; this.score = score; } public String getName(){ return name; } public double getScore(){ return score; } public int compareTo(Object o1){ Student s1 = (Student)o1; if ( this.score > s1.getScore() ) return 1; if ( this.score < s1.getScore() ) return -1; return 0; } /* Comparator接口 public int compare(Object o1,Object o2){ Student s1 = (Student)o1; Student s2 = (Student)o2; if ( s1.getScore() > s2.getScore() ) return 1; if ( s1.getScore() < s2.getScore() ) return -1; return 0; } */ public static void main(String[] args){ Vector stuVector = new Vector(); try { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); Student s; String s1=""; String s2=""; System.out.println("Please input the 5 students' name and score!"); for (int i=0; i<5; i++){ s1 = in.readLine() ; s2 = in.readLine() ; s = new Student(s1, Double.valueOf(s2).doubleValue()); stuVector.add(s); System.out.println(" a student added! name:"+ s1 + " score:"+s2); } } catch(Exception e) { e.printStackTrace(); } Collections.sort(stuVector); System.out.println("The ordered Student name:"); for(int i = 0;i<stuVector.size();i++){ Student s = (Student)stuVector.get(i); System.out.println(" " + s.getName()); } //System.in.read(); } } Arrays.sort(...)Collection.sort();YourOwn.sort(...) :P linux(starx)系统安装jdk1.5出错,求大神解决 接口问题 请高手指教 在Eclipse中如何让java源代码和中文提示共存 创建一个[10][8]的数组太大? 哪位高人能帮我解释一下关于地址和引用传递的问题?800里加急,在线等待回复!(用分砸死你) 不一样的 package 问题 关于Java与SQL的问题 如何得到最底层的容器? 对于java我们应该还要做点什么? 关于右移运算符>> 关于"for (String g greeting)"的用法不能运行在JSDK1.5_??中的问题
然后用java.util.Arrays类的sort方法实现排序Vector v;
//....
java.util.Arrays.sort(v.toArray(),comparator);
import java.io.*;public class Student implements Comparable{
private String name ="";
private double score = 0;
public Student (String name, double score){
this.name = name;
this.score = score;
}
public String getName(){
return name;
}
public double getScore(){
return score;
}
public int compareTo(Object o1){
Student s1 = (Student)o1;
if ( this.score > s1.getScore() ) return 1;
if ( this.score < s1.getScore() ) return -1;
return 0;
}
/* Comparator接口
public int compare(Object o1,Object o2){
Student s1 = (Student)o1;
Student s2 = (Student)o2;
if ( s1.getScore() > s2.getScore() ) return 1;
if ( s1.getScore() < s2.getScore() ) return -1;
return 0;
}
*/
public static void main(String[] args){
Vector stuVector = new Vector();
try
{
BufferedReader in =
new BufferedReader(
new InputStreamReader(System.in));
Student s;
String s1="";
String s2="";
System.out.println("Please input the 5 students' name and score!");
for (int i=0; i<5; i++){
s1 = in.readLine() ;
s2 = in.readLine() ;
s = new Student(s1, Double.valueOf(s2).doubleValue());
stuVector.add(s);
System.out.println(" a student added! name:"+ s1 + " score:"+s2);
} }
catch(Exception e)
{
e.printStackTrace();
}
Collections.sort(stuVector);
System.out.println("The ordered Student name:");
for(int i = 0;i<stuVector.size();i++){
Student s = (Student)stuVector.get(i);
System.out.println(" " + s.getName());
}
//System.in.read();
}
}
Collection.sort();
YourOwn.sort(...) :P