本帖最后由 小羽天空 于 2013-6-11 13:05 编辑
import java.util.*;
public class Test10 {
/**
* @param args
*/
public static void main(String[] args) {
TreeSet<student> t = new TreeSet<student>(new comparacout());
t.add(new student("zhangsan",18,88));
//t.add(new student("lisi",19,98));
//t.add(new student("wangwu",18,89));
//t.add(new student("zhangsan",21,90));
//t.add(new student("zhaoliu",18,88));
Iterator<student> it = t.iterator();
while(it.hasNext())
{
//Student stu = (Student)it.next();
//System.out.println(stu.toString());
System.out.println(it.next().toString());
}
}
}
class comparacout implements Comparator
{
public int compare(Object o1, Object o2) {
student s1 = (student)o1;
student s2 = (student)o2;
int num = new Integer(s1.getcount()).compareTo(new Integer(s2.getcount()));
if(num==0)
return s1.compareTo(s2);
return num;
}
}
class student implements Comparable<student>
{
private String name;
private int age;
private int count;
public student(String name,int age,int count)
{
this.name = name;
this.age = age;
this.count = count;
}
public int HashCode()
{
return name.hashCode()+age+34;
}
public boolean equals(Object s)
{
if(!(s instanceof student) )
throw new ClassCastException("class dont't like");
student d = (student)s;
return this.name.equals(d.name) && this.age==d.age;
}
public int compareTo(student s) {
int num = this.name.compareTo(s.name);
if(num==0)
return new Integer(this.age).compareTo(new Integer(s.age));
return num;
}
String getname()
{
return this.name;
}
int getnage()
{
return this.age;
}
int getcount()
{
return this.count;
}
public String toString()
{
return name+":"+age+" :count :"+count;
}
}
他会报错误:但总觉得没错啊,好晕啊!
Exception in thread "main" java.lang.NoClassDefFoundError: com/itheima/student (wrong name: com/itheima/Student)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at com.itheima.Test10.main(Test10.java:16)
|