正在回答
3回答
你好同学,问题的主要原因出在了你的hashcode方法上,你的hashcode方法没有result = prime * result + ((name == null) ? 0 : name.hashCode());对名字进行判断的胆码, 建议同学使用如下方式进行创建,按Alt+Shift+S,然后选择下图标记的选项来创建方法。这样不会出现遗漏的错误。

修订之后的代码如下,祝学习愉快~
public class Cat {
private String name; //名字
private int month; //年龄
private String species;//品种
//构造方法
public Cat(String name, int month, String species) {
super();
this.name = name;
this.month = month;
this.species = species;
}
//getter与setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
@Override
public String toString() {
return "[姓名:" + name + ", 年龄:" + month + ", 品种:" + species + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + month;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((species == null) ? 0 : species.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Cat other = (Cat) obj;
if (month != other.month)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (species == null) {
if (other.species != null)
return false;
} else if (!species.equals(other.species))
return false;
return true;
}
}
巴呆丶
2019-03-14 17:42:44
public class Cat {
private String name; //姓名
private int month; //年龄
private String specise; //品种
public Cat() {
}
public Cat(String name, int month, String specise) {
this.name = name;
this.month = month;
this.specise = specise;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getSpecise() {
return specise;
}
public void setSpecise(String specise) {
this.specise = specise;
}
@Override
public String toString() {
return "{姓名:" + name + ", 年龄: " + month + ", 品种: " + specise+"}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Cat)) return false;
Cat cat = (Cat) o;
if (getMonth() != cat.getMonth()) return false;
if (getName() != null ? !getName().equals(cat.getName()) : cat.getName() != null) return false;
return getSpecise() != null ? getSpecise().equals(cat.getSpecise()) : cat.getSpecise() == null;
}
@Override
public int hashCode() {
int result = getName() != null ? getName().hashCode() : 0;
result = 31 * result + getMonth();
result = 31 * result + (getSpecise() != null ? getSpecise().hashCode() : 0);
return result;
}
}
//=============================================================
public class CatTest {
public static void main(String[] args) {
Cat one = new Cat("花花",12,"英国短毛猫");
Cat two = new Cat("凡凡",3,"中华田园猫");
Set set = new HashSet();
set.add(one);
set.add(two);
//打印输出宠物猫信息
Iterator it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("*********************");
Cat once = new Cat("花花",12,"英国短毛猫");
set.add(once);
System.out.println("添加重复信息后的宠物猫信息:");
it = set.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
System.out.println("**********************");
//插入一个新的宠物猫
Cat one2 = new Cat("花花二代",2,"英国短毛猫");
set.add(one2);
System.out.println("添加花花二代后的宠物猫信息: ");
it = set.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
System.out.println("**********************");
//在集合中查找花花的信息并输出
if(set.contains(one)) {
System.out.println("花花找到了");
System.out.println(one);
}else {
System.out.println("花花没找到");
}
//根据集合中的花花名字查找信息
boolean flag = false; //定义一个值为false的Boolean类型变量flag
Cat c = null; //定义一个Cat对象初始化内容为null,方便在后面的输出语句中调用
it = set.iterator(); //重置迭代器
while (it.hasNext()) { //遍历数组
c = (Cat) it.next(); //将数组内容传给对象c
if (c.getName().equals("花花")) { //判断对象中getName获得的姓名是否等于”花花“
flag = true; //如果等于,则找到了,将flag重新赋值为true
break; //跳出循环
}
}
if (flag) { //如果flag为true
System.out.println("花花找到了"); //输出字符串”花花找到了“
System.out.println(c); //输出此时c中的内容
}else {
System.out.println("花花没找到"); //flag为false输入字符串"花花没找到"
}
}
}
chrismorgen
2019-03-14 17:36:43
你好同学,请问你的代码是如何写的呢?你应该是没有排序吧,建议同学将你的代码粘贴到“我要回答中”,否则会失去代码格式,这样方便老师为你修改错误,祝学习愉快~
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星