List list = new ArrayList();
list.add(new Person("lcl", 28));
Person person=new Person("wqx", 29);
list.add(person);
list.add(new Person("kx", 23));
System.out.println(Collections.binarySearch(list,person));//错误在这里
为什么会出现以下异常:
java.lang.ClassCastException
at java.util.Collections.indexedBinarySearch(Collections.java:210)
at java.util.Collections.binarySearch(Collections.java:198)
at util.ListSort.main(ListSort.java:36)
list.add(new Person("lcl", 28));
Person person=new Person("wqx", 29);
list.add(person);
list.add(new Person("kx", 23));
System.out.println(Collections.binarySearch(list,person));//错误在这里
为什么会出现以下异常:
java.lang.ClassCastException
at java.util.Collections.indexedBinarySearch(Collections.java:210)
at java.util.Collections.binarySearch(Collections.java:198)
at util.ListSort.main(ListSort.java:36)
ArrayList list = new ArrayList();
list.add(new Person("lcl", 28));
Person person=new Person("wqx", 29);
list.add(person);
list.add(new Person("kx", 23));
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
Person p = (Person) list.get(i);
System.out.print(p.getName() + " ");
System.out.println(p.getAge());
}
{
String name = "";
Integer ageInt = null;
//抽象方法的继承
public int compareTo(Object o)
{
return compareToThis((Integer)o);
}
public int compareToThis(Integer anotherInteger)
{
int i = anotherInteger.compareTo(ageInt);
return i;
}
public Person(String name, Integer age)
{
this.name = name;
//this.age = age;
this.ageInt = age;
}
public String getName()
{
return this.name;
}
public Integer getAge()
{
return this.ageInt;
}
}