当前位置: 技术问答>java相关
帮帮忙。很简单的
来源: 互联网 发布时间:2017-04-08
本文导语: import java.util.*; public class ArrayListDeml { public static void main(String[] args){ User user1=new User("chen","6898397"); User user2=new User("jean","6296336"); User user3=new User("tao","1111111"); ArrayList userlist=new Array...
import java.util.*;
public class ArrayListDeml {
public static void main(String[] args){
User user1=new User("chen","6898397");
User user2=new User("jean","6296336");
User user3=new User("tao","1111111");
ArrayList userlist=new ArrayList();
userlist.add(user1);
userlist.add(user2);
userlist.add(user3);
Collections.sort(userlist);
Iterator iterator=userlist.iterator();
while(iterator.hasNext()){
User user=(User)iterator.next();
System.out.println(user.getUsername());
}
}
}
报错:
java.lang.ClassCastException: untitled6.User
at java.util.Arrays.mergeSort(Arrays.java:1064)
at java.util.Arrays.sort(Arrays.java:1014)
at java.util.Collections.sort(Collections.java:78)
at untitled6.ArrayListDeml.main(ArrayListDeml.java:13)
Exception in thread "main"
public class ArrayListDeml {
public static void main(String[] args){
User user1=new User("chen","6898397");
User user2=new User("jean","6296336");
User user3=new User("tao","1111111");
ArrayList userlist=new ArrayList();
userlist.add(user1);
userlist.add(user2);
userlist.add(user3);
Collections.sort(userlist);
Iterator iterator=userlist.iterator();
while(iterator.hasNext()){
User user=(User)iterator.next();
System.out.println(user.getUsername());
}
}
}
报错:
java.lang.ClassCastException: untitled6.User
at java.util.Arrays.mergeSort(Arrays.java:1064)
at java.util.Arrays.sort(Arrays.java:1014)
at java.util.Collections.sort(Collections.java:78)
at untitled6.ArrayListDeml.main(ArrayListDeml.java:13)
Exception in thread "main"
|
User user1//User类没有放在同一目录,并且没有 import进来.
|
没有找到User类
|
问题出在这句话:
Collections.sort(userlist);
对象排序,应该implements接口Comparable
Collections.sort(userlist);
对象排序,应该implements接口Comparable
|
你的user类没有实现Comparable接口!!!!!!!!!