标题:[求助]HashSet为什么存上相同的内容了??
取消只看楼主
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
 问题点数:0 回复次数:1 
[求助]HashSet为什么存上相同的内容了??



文件:HashSetTest.java


import java.util.*;

public class HashSetTest
{
public static void main(String[] args)
{
HashSet<Student> hs = new HashSet<Student>();
hs.add(new Student("Dreamm",20)); //重复的
hs.add(new Student("张三",20));
hs.add(new Student("Dreamm",20)); //重复的
hs.add(new Student("Dreamming",20));
hs.add(new Student("MY.L",20));

for(Student s : hs)
{
System.out.println("name: " + s.getName());
System.out.println("age: " + s.getAge());
}

System.out.println("长度是:" + hs.size()); //这里为什么会打印5啊?hs中有一对重复的呀,不是应该4吗?
}
}

class Student
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

搜索更多相关主题的帖子: HashSet 
2007-03-07 19:40
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
得分:0 

噢耶!明白了,谢二位~


我在说's blog   http://www..cn
2007-03-07 22:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-122953-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.060238 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved