黑马程序员技术交流社区
标题:
我自己写的Student类和存储方式,请大牛们指点
[打印本页]
作者:
韩伟
时间:
2012-7-26 10:14
标题:
我自己写的Student类和存储方式,请大牛们指点
一下是小弟写的Student类和关于对象的各种存储,请给位大牛给指点一下!
<p>class Student implements Comparable<Student>
{
private String name;
private int age;
private String num;
public Student() {}
public Student(String name, int age, String num)
{
this.name = name;
this.age = age;
this.num = num;
}
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public void setNum(String num)
{
this.num = num;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String getNum()
{
return num;
}
public void show()
{
System.out.println("name:"+name+" age:"+age+" num:"+num);
}
public int hashCode()
{
return num.hashCode();
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("不是Student类");
Student s = (Student)obj;
return this.getName().equals(s.getName())&&this.getAge() == s.getAge();
}
public int compareTo(Student s)
{
int n = this.getName().compareTo(s.getName());
if(0 == n)
return s.getAge() - this.getAge();
return n;
}
}
</p><div class="blockcode"><blockquote>import java.util.*;
class MyStudentTest
{
public static void main(String []args)
{
ArrayList <Student> al = new ArrayList<Student>();
al.add(new Student("bbb",13,"2001002"));
al.add(new Student("ccc",15,"2001003"));
al.add(new Student("aaa",12,"2001001"));
al.add(new Student("ccc",18,"2001005"));
Iterator<Student> it = al.iterator();
while(it.hasNext())
{
it.next().show();
}
LinkedList <Student> li = new LinkedList<Student>();
li.addFirst(new Student("孙悟空",11200,"2001011"));
li.addLast(new Student("猪八戒",1300,"2001022"));
li.addFirst(new Student("沙悟净",1400,"2001033"));
li.addFirst(new Student("小白龙",13120,"2001007"));
Iterator <Student> it2 = li.iterator();
while(it2.hasNext())
{
it2.next().show();
}
HashSet <Student> hs = new HashSet<Student>();
hs.add(new Student("贾宝玉",20,"2002123"));
hs.add(new Student("薛宝钗",19,"2002234"));
hs.add(new Student("林黛玉",18,"2002345"));
hs.add(new Student("王熙凤",20,"2002456"));
hs.add(new Student("林黛玉",18,"2002345"));
Iterator<Student> it3 = hs.iterator();
while(it3.hasNext())
{
it3.next().show();
}
TreeSet <Student> ts = new TreeSet<Student>();
ts.add(new Student("贾宝玉",20,"2002123"));
ts.add(new Student("薛宝钗",19,"2002234"));
ts.add(new Student("林黛玉",18,"2002345"));
ts.add(new Student("王熙凤",20,"2002456"));
Iterator <Student>it4 = ts.iterator();
while(it4.hasNext())
{
it4.next().show();
}
HashMap<String,Student> hm = new HashMap<String,Student>();
hm.put("first",new Student("贾宝玉",20,"2002123"));
hm.put("second",new Student("薛宝钗",19,"2002234"));
hm.put("thrid",new Student("林黛玉",18,"2002345"));
hm.put("fouth",new Student("薛宝钗",19,"2002234"));
Set<String> set1 = hm.keySet();
Iterator <String> it5 = set1.iterator();
while(it5.hasNext())
{
String key = it5.next();
System.out.print(key+"...");
hm.get(key).show();
}
Set<Map.Entry<String,Student>> set2 = hm.entrySet();
Iterator<Map.Entry<String,Student>> it6 =set2.iterator();
while(it6.hasNext())
{
Map.Entry<String,Student> me1 =it6.next();
System.out.print(me1.getKey()+" ");
me1.getValue().show();
}
TreeMap <String, Student> tm = new TreeMap<String,Student>();
tm.put("first",new Student("贾宝玉",20,"2002123"));
tm.put("second",new Student("薛宝钗",19,"2002234"));
tm.put("thrid",new Student("林黛玉",18,"2002345"));
tm.put("fouth",new Student("薛宝钗",19,"2002234"));
Set<String> set3 = tm.keySet();
Iterator<String> it7 = set3.iterator();
while(it7.hasNext())
{
String key = it7.next();
System.out.print(key+" ");
tm.get(key).show();
}
Set<Map.Entry<String,Student>> mapentry = tm.entrySet();
Iterator<Map.Entry<String,Student>> it8 = mapentry.iterator();
while(it2.hasNext())
{
Map.Entry<String,Student> me2 = it8.next();
System.out.print(me2.getKey()+" ");
me2.getValue().show();
}
}
}
复制代码
作者:
黑马连家华
时间:
2012-7-27 16:35
不错,好多人我都不认识,你还知道他年龄编号啊{:soso_e106:}
作者:
柳雷
时间:
2012-7-27 17:02
{:soso_e120:}
作者:
王渠
时间:
2012-7-27 17:17
指出错误的地方,hashCode方法你用的是系统默认的方法,这里就可以看出来是错了。学生中,如果出现了同名,同年龄的是否判断为同一个对象呢???
如果判断为同一个对象,那么这个hashCode却认为这两个对象是不同的,存入了集合中。由此出现了错误。
需要复写hashCode方法。
public int hashCode{
return 1;//这里可以随意的写一个值,因为当判断到哈希值相同后,还要去判断equals方法,这样就可以确保对象的唯一性。
}
要保证自定义的类的对象的唯一性,需要复写hashCode方法和equals两个方法。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2