vector 修改 java_java对vector动态数组中的对象排序,以下代码有何问题,如何修改?...
展開全部
package com.tx.collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
public class Student {
String name;
int score;
public Student(String n, int s) {
name = n;
score = s;
}
public boolean equals(Object o) {
Student other = (Student) o;
return other.score == score;
}
public String toString() {
return name + "," + score;
}
public static void main(String[] args) {
Vector stu = new Vector();
stu.add(new Student("張三", 90));
stu.add(new Student("李四", 80));
stu.add(new Student("小明32313133353236313431303231363533e58685e5aeb931333363386231", 85));
stu.add(new Student("王五", 60));
stu.add(new Student("何六", 70));
Student index = new Student("", 60);
int t = stu.indexOf(index);
System.out.println("成績?yōu)?0的學(xué)生姓名為" + stu.elementAt(t).name);
Set set = new TreeSet(new Comparator() {
@Override
public int compare(Student o1, Student o2) {
if (o1.score > o2.score)
return -1;
else if (o1.score < o2.score)
return 1;
else
return 0;
}
});
set.addAll(stu);
Iterator iterator = set.iterator();
while(iterator.hasNext()){
Student s = iterator.next();
System.out.println(s.name+":"+s.score);
}
}
}
總結(jié)
以上是生活随笔為你收集整理的vector 修改 java_java对vector动态数组中的对象排序,以下代码有何问题,如何修改?...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转载] Java序列化的几种方式以及序
- 下一篇: 威纶通触摸屏与mysql_威纶触摸屏应用