我想问一下import java.util.*; public class HashSetTest02 { public static void main(String[] args)
importjava.util.*;publicclassHashSetTest02{publicstaticvoidmain(String[]args){Personp...
import java.util.*;
public class HashSetTest02 {
publicstatic void main(String[] args) {
Person p1 = new Person();
p1.name = "张三";
p1.age = 20;
Person p2 = new Person();
p2.name = "李四";
p2.age = 30;
Person p3 = new Person();
p3.name = "张三";
p3.age = 40;
Set set = new HashSet();
set.add(p1);
set.add(p2);
set.add(p3);
for (Iterator iter=set.iterator();iter.hasNext();) {
Personp = (Person)iter.next();
System.out.println("name="+ p.name + ", age=" + p.age);
}
System.out.println("p1.hashCode="+ p1.hashCode());
System.out.println("p2.hashCode="+ p2.hashCode());
System.out.println("p3.hashCode="+ p3.hashCode());
}
}
class Person {
Stringname;
intage;
我想问一下那个hashcode码是怎么冒出来的,我从程序当中找了半天也没找见 展开
public class HashSetTest02 {
publicstatic void main(String[] args) {
Person p1 = new Person();
p1.name = "张三";
p1.age = 20;
Person p2 = new Person();
p2.name = "李四";
p2.age = 30;
Person p3 = new Person();
p3.name = "张三";
p3.age = 40;
Set set = new HashSet();
set.add(p1);
set.add(p2);
set.add(p3);
for (Iterator iter=set.iterator();iter.hasNext();) {
Personp = (Person)iter.next();
System.out.println("name="+ p.name + ", age=" + p.age);
}
System.out.println("p1.hashCode="+ p1.hashCode());
System.out.println("p2.hashCode="+ p2.hashCode());
System.out.println("p3.hashCode="+ p3.hashCode());
}
}
class Person {
Stringname;
intage;
我想问一下那个hashcode码是怎么冒出来的,我从程序当中找了半天也没找见 展开
展开全部
System.out.println("p1.hashCode="+ p1.hashCode());
System.out.println("p2.hashCode="+ p2.hashCode());
System.out.println("p3.hashCode="+ p3.hashCode());
就是这3句话
System.out.println("p2.hashCode="+ p2.hashCode());
System.out.println("p3.hashCode="+ p3.hashCode());
就是这3句话
更多追问追答
追问
我也看出来在这了可是在这也没数字啊
追答
哈希码是自动生成的
p2.hashCode();
调用了hashCode()方法,这个方法自动生成哈希码
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询