大家好。请帮我看看写的代码错在那里?
import java.util.*;
public class HashSetTest
{
public static void main(String[] args)
{
TreeSet<Student> hs=new TreeSet<Student>();
hs.add(new Student("zhangsan"));
hs.add(new Student("lisi"));
hs.add(new Student("wangwu"));
hs.add(new Student("zhangsan")); System.out.println(hs);
System.out.println(hs.size());
}
} class Student implements Comparable
{
String name;
Student(String name)
{
this.name=name;
}
public String toString()
{
return name;
}
public int compareTo(Object p)
{
int age = name.compareTo((Student)p.name);
return (age == 0 ? name.compareTo((Student)p.name) : age);
}
}
import java.util.*;
public class HashSetTest
{
public static void main(String[] args)
{
TreeSet<Student> hs=new TreeSet<Student>();
hs.add(new Student("zhangsan"));
hs.add(new Student("lisi"));
hs.add(new Student("wangwu"));
hs.add(new Student("zhangsan")); System.out.println(hs);
System.out.println(hs.size());
}
} class Student implements Comparable
{
String name;
Student(String name)
{
this.name=name;
}
public String toString()
{
return name;
}
public int compareTo(Object p)
{
int age = name.compareTo((Student)p.name);
return (age == 0 ? name.compareTo((Student)p.name) : age);
}
}
public int compareTo(Object p)
{
int age = name.compareTo((Student)p.name);
return (age == 0 ? name.compareTo((Student)p.name) : age);
}
[/code]
是这个?[code=Jav]public int compareTo(Object p)
{
int age = name.compareTo(((Student)p).name);
return (age == 0 ? name.compareTo(((Student)p).name) : age);
}
[/code]
[code=Jav]
public int compareTo(Object p)
{
int age = name.compareTo((Student)p.name);
return (age == 0 ? name.compareTo((Student)p.name) : age);
}
[/code]
是这个?[code=Jav]public int compareTo(Object p)
{
int age = name.compareTo(((Student)p).name);
return (age == 0 ? name.compareTo(((Student)p).name) : age);
}
[/code]