java collections.sort
importjava.util.*;publicclassListtest{publicstaticvoidmain(Stringargs[]){Listl1=newLi...
import java.util.*;
public class Listtest{
public static void main(String args[]){
List l1 = new LinkedList();
l1.add(new name("ac","A"));
l1.add(new name("uang","u"));
name n = new name("ad","A");
//int x = n.compareTo(new name("a2","B"));
l1.add(new name("at","t"));
l1.add(new name("louis","uang"));
l1.add(new name("guanshi","yang"));
Collections.sort(l1);
System.out.println(l1);
//System.out.println(x);
}
}
class name{
private String firstname;
private String lastname;
public name(String firstname,String lastname){
this.firstname = firstname ;
this.lastname = lastname;
}
public String getfirstname(){
return firstname;
}
public String getlastname(){
return lastname;
}
public String toString(){
return firstname + " " + lastname;
}
public void setfirstname(String firstname){
this.firstname = firstname ;
}
public void setlastname(String lastname){
this.lastname = lastname ;
}
public boolean equals(Object obj){
boolean ben = false;
if(obj instanceof name){
name name = (name)obj;
if(this.lastname == name.lastname){
ben = true;
ben = ben?this.firstname==name.firstname:false;
return ben;
}
}
return super.equals(obj);
}
public int hashCode(){
String str = new String(lastname);
return str.hashCode();
}
int compareTo(name o){
name name = (name)o ;
int k = lastname.compareTo(name.lastname);
return (k != 0? k : firstname.compareTo(name.firstname));
}
}
程序运行报错:不可比较的错误。不知道问题出在哪里 展开
public class Listtest{
public static void main(String args[]){
List l1 = new LinkedList();
l1.add(new name("ac","A"));
l1.add(new name("uang","u"));
name n = new name("ad","A");
//int x = n.compareTo(new name("a2","B"));
l1.add(new name("at","t"));
l1.add(new name("louis","uang"));
l1.add(new name("guanshi","yang"));
Collections.sort(l1);
System.out.println(l1);
//System.out.println(x);
}
}
class name{
private String firstname;
private String lastname;
public name(String firstname,String lastname){
this.firstname = firstname ;
this.lastname = lastname;
}
public String getfirstname(){
return firstname;
}
public String getlastname(){
return lastname;
}
public String toString(){
return firstname + " " + lastname;
}
public void setfirstname(String firstname){
this.firstname = firstname ;
}
public void setlastname(String lastname){
this.lastname = lastname ;
}
public boolean equals(Object obj){
boolean ben = false;
if(obj instanceof name){
name name = (name)obj;
if(this.lastname == name.lastname){
ben = true;
ben = ben?this.firstname==name.firstname:false;
return ben;
}
}
return super.equals(obj);
}
public int hashCode(){
String str = new String(lastname);
return str.hashCode();
}
int compareTo(name o){
name name = (name)o ;
int k = lastname.compareTo(name.lastname);
return (k != 0? k : firstname.compareTo(name.firstname));
}
}
程序运行报错:不可比较的错误。不知道问题出在哪里 展开
展开全部
Collections的sort(List)方法要求参数必须实现Comparable接口
你的name类没有实现,所以不能运行
这样改class name implements Comparable {
然后把最后一个
int compareTo(name o){
name name = (name)o ;
int k = lastname.compareTo(name.lastname);
return (k != 0? k : firstname.compareTo(name.firstname));
}
改成
public int compareTo(Object o){
然后在函数里面把o转换成name类型的
你的name类没有实现,所以不能运行
这样改class name implements Comparable {
然后把最后一个
int compareTo(name o){
name name = (name)o ;
int k = lastname.compareTo(name.lastname);
return (k != 0? k : firstname.compareTo(name.firstname));
}
改成
public int compareTo(Object o){
然后在函数里面把o转换成name类型的
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询