在java中迭代器it.next()返回的是什么?下面的代码中为什么输出是地址值?而不是具体对象加属性值?
publicclassSet_method{publicstaticvoidmain(String[]args){Set<Student>set=newHashSet<S...
public class Set_method {
public static void main(String [] args)
{
Set<Student> set = new HashSet<Student>();
set.add(new Student("lisi1",22));
set.add(new Student("lisi2",22));
set.add(new Student("lisi3",24));
set.add(new Student("lisi5",27));
set.add(new Student("lisi2",21));
set.add(new Student("lisi3",23));
//注意的是该容器只能用迭代器取元素
for(Iterator<Student> it =set.iterator(); it.hasNext();)
{
System.out.println(it.next().getClass());
System.out.println(it.next());
}
}
}
//输出:
class Student
Student@623686a
class Student
Student@6236869
class Student
Student@6236908 展开
public static void main(String [] args)
{
Set<Student> set = new HashSet<Student>();
set.add(new Student("lisi1",22));
set.add(new Student("lisi2",22));
set.add(new Student("lisi3",24));
set.add(new Student("lisi5",27));
set.add(new Student("lisi2",21));
set.add(new Student("lisi3",23));
//注意的是该容器只能用迭代器取元素
for(Iterator<Student> it =set.iterator(); it.hasNext();)
{
System.out.println(it.next().getClass());
System.out.println(it.next());
}
}
}
//输出:
class Student
Student@623686a
class Student
Student@6236869
class Student
Student@6236908 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询