import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public class testSort {
public static void main(String[] args) {
Student stu1=new Student("zhangsan",18);
Student stu2=new Student("lisi",20);
Student stu3=new Student("wangba",31);
Student stu4=new Student("zhaoliu",17);
List<Student> stu=new ArrayList<Student>();
stu.add(stu1);
stu.add(stu2);
stu.add(stu3);
stu.add(stu4);
System.out.println("原始数据:");
for (Student student : stu) {
System.out.println(student);
}
System.out.println("进行排序......");
ComparatorSort com=new ComparatorSort();
Collections.sort(stu, com);
for (Student student : stu) {
System.out.println(student);
}
Student maxstu=Collections.max(stu);
System.out.println(maxstu);
}
}
这段代码前面排序都好使,就max出错误,说是max括号内的类型不匹配.stu应当继承自一个泛型.那就是Colections.max()无法使用.
那怎么使用Comparator创建对象,再用CollectionS.max进行排序啊?
import java.util.Collections;
import java.util.List;public class testSort {
public static void main(String[] args) {
Student stu1=new Student("zhangsan",18);
Student stu2=new Student("lisi",20);
Student stu3=new Student("wangba",31);
Student stu4=new Student("zhaoliu",17);
List<Student> stu=new ArrayList<Student>();
stu.add(stu1);
stu.add(stu2);
stu.add(stu3);
stu.add(stu4);
System.out.println("原始数据:");
for (Student student : stu) {
System.out.println(student);
}
System.out.println("进行排序......");
ComparatorSort com=new ComparatorSort();
Collections.sort(stu, com);
for (Student student : stu) {
System.out.println(student);
}
Student maxstu=Collections.max(stu);
System.out.println(maxstu);
}
}
这段代码前面排序都好使,就max出错误,说是max括号内的类型不匹配.stu应当继承自一个泛型.那就是Colections.max()无法使用.
那怎么使用Comparator创建对象,再用CollectionS.max进行排序啊?
解决方案 »
- JAVA 方向 做SWING 产品 有发展么?
- 如何将new Date()保存起来作为下次更新的起始日期(除了存放在数据库)
- 有人见过这个奇怪的错误吗?执行起始页面的时候就抱这个错,后台一点错都没有!
- include file参数问题
- 关于在java.awt.canvas画布中查找的问题
- 怎么用DecimalFormat实现123000123变成123,000,123呢?在线等
- jdk版本问题~~~~~~~
- 如何获得系统的所有字符集
- 关于bufferedimage中重画的问题
- URL中的汉字那里来的啊,如 问题=%E9%97%AE%E9%A2%98,怎么转换来的
- 继续请教序列化为二进制字节流的问题!
- java swing JTree 如何设置子节点图片跟父节点图片对齐。
public static <T extends java/lang/Object> T max(java.util.Collection<? extends T>, java.util.Comparator<? super T>);你的调用符合max的签名吗?
public int compareTo(Student that){
....
}
}
class ComparatorSort extends Comparator<Student> {
public int compare(Student s1, Student s2){
....
}
}
public int compare(Student o1, Student o2) {
return (o1.age < o2.age ? -1 : (o1.age == o2.age ? 0 : 1));
}
}public class testSort {
public static void main(String[] args) {
Student stu1 = new Student("zhangsan", 18);
Student stu2 = new Student("lisi", 20);
Student stu3 = new Student("wangba", 31);
Student stu4 = new Student("zhaoliu", 17); List<Student> stu = new ArrayList<Student>();
stu.add(stu1);
stu.add(stu2);
stu.add(stu3);
stu.add(stu4); System.out.println("原始数据:");
for (Student student : stu) {
System.out.println(student);
} System.out.println("进行排序......");
StudentComparator studentComparator=new StudentComparator();
Collections.sort(stu, studentComparator);
for (Student student : stu) {
System.out.println(student);
} Student maxstu = Collections.max(stu,studentComparator);
System.out.println(maxstu);
}
}
import java.util.Collections;
import java.util.Comparator;
import java.util.List;class Student{
String name;
int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String toString(){
return name + " " + age;
}
}class StudentComparator implements Comparator<Student> {
public int compare(Student o1, Student o2) {
return (o1.age < o2.age ? -1 : (o1.age == o2.age ? 0 : 1));
}
}public class testSort {
public static void main(String[] args) {
Student stu1 = new Student("zhangsan", 18);
Student stu2 = new Student("lisi", 20);
Student stu3 = new Student("wangba", 31);
Student stu4 = new Student("zhaoliu", 17); List<Student> stu = new ArrayList<Student>();
stu.add(stu1);
stu.add(stu2);
stu.add(stu3);
stu.add(stu4); System.out.println("原始数据:");
for (Student student : stu) {
System.out.println(student);
} System.out.println("进行排序......");
StudentComparator studentComparator=new StudentComparator();
Collections.sort(stu, studentComparator);
for (Student student : stu) {
System.out.println(student);
} Student maxstu = Collections.max(stu,studentComparator);
System.out.println("年龄最大" + maxstu);
}
}
/*
原始数据:
zhangsan 18
lisi 20
wangba 31
zhaoliu 17
进行排序......
zhaoliu 17
zhangsan 18
lisi 20
wangba 31
年龄最大wangba 31
*/
谢谢你们两个了,我自己学Java有时候就遇到不会的没办法.谢谢了!