黑马程序员技术交流社区
标题:
迭代器的问题
[打印本页]
作者:
wyy666
时间:
2014-5-31 23:58
标题:
迭代器的问题
import java.util.*;
class HashSetDemo
{
public static void main(String[] args)
{
HashSet ht = new HashSet();
ht.add(new Student("小五",19));
ht.add(new Student("地方",139));
ht.add(new Student("小df",129));
ht.add(new Student("小dfaaadf",197));
Iterator it = ht.iterator();
while(it.hasNext())
{
System.out.println(it.next().name);
/*提示找不到符号,为什么it.next().getClass()方法返回的是Student对象,为什么it.next().name这条语句会出现错误*/
}
}
}
class Student
{
public String name;
public int num;
public Student(String name,int num)
{
this.name = name;
this.num = num;
}
}
作者:
superob123
时间:
2014-6-1 00:47
因为没有加泛型,HashSet集合是不知道开发这往里面加什么数据的,所以提示找不到Student。把代码改成以下代码就可以了。
package test1;
import java.util.*;
public class HashSetDemo {
public static void main(String[] args) {
HashSet<Student> ht = new HashSet();
ht.add(new Student("小五",19));
ht.add(new Student("地方",139));
ht.add(new Student("小df",129));
ht.add(new Student("小dfaaadf",197));
Iterator<Student> it = ht.iterator();
while(it.hasNext())
{
System.out.println(it.next().name);
}
}
}
class Student
{
public String name;
public int num;
public Student(String name,int num)
{
this.name = name;
this.num = num;
}
}
复制代码
望采纳,另外求吧主给点技术分吧,新人不容易啊
作者:
李小然
时间:
2014-6-1 02:49
本帖最后由 李小然 于 2014-6-1 02:53 编辑
HashSet中的boolean add()方法中的参数可以理解为Object
即声明为:boolean add(Object obj)
在这样的情况下,
你用ht.add(new Student("小五",19));向HashSet中添加元素,
相当于中间有一句
Object obj = new Student("小五",19));
父类引用指向子类对象,这是典型的多态问题。
而Iterator it = ht.iterator();
it.next()相当于拿到的是每个obj的引用。
这就解释了为什么你用it.next().getClass()得到的是Student类(因为是new的Student),
但是却调用不了getName()方法(父类引用无法调用子类特有方法)。
:dizzy:不知道代码为什么总是抽,放在下面的楼中..
作者:
李小然
时间:
2014-6-1 02:54
本帖最后由 李小然 于 2014-6-1 02:56 编辑
以下是解决办法:
import java.util.*;
class HashSetDemo
{
public static void main(String[] args)
{
HashSet ht = new HashSet();
ht.add(new Student("小五",19));
ht.add(new Student("地方",139));
ht.add(new Student("小df",129));
ht.add(new Student("小dfaaadf",197));
Iterator it = ht.iterator();
while(it.hasNext())
{
//将obj类型强转为Student类型
Student st = (Student)it.next();
System.out.println("Name: " +
st
.name +" Num: " +
st
.num);
}
}
}
class Student
{
public String name;
public int num;
public Student(String name,int num)
{
this.name = name;
this.num = num;
}
}
作者:
格子、
时间:
2014-6-1 08:01
/*提示找不到符号,为什么it.next().getClass()方法返回的是Student对象,为什么it.next().name这条语句会出现错误*/之所以会这样是因为,迭代器在返回元素的时候对类类型进行了默认的类型提升,也就是Student类被提升了为了Object类,所以Student类中的name属性就不能调用了,getClass()方法返回Student-body这也是多态的一种体现。想得到Student中的那么属性,只要做一下强转动作就行,把返回的Object类在给强转回Student类即可,楼主可以这样改:
import java.util.*;
class HashSetDemo
{
public static void main(String[] args)
{
HashSet ht = new HashSet();
ht.add(new Student("小五",19));
ht.add(new Student("地方",139));
ht.add(new Student("小df",129));
ht.add(new Student("小dfaaadf",197));
Iterator it = ht.iterator();
while(it.hasNext())
{
//<font color="Red">在调用Student的属性之前,我们先把它强转为Student类就可以了 Object类中没有name属性才会报错 找不到符号!!!</font>
System.out.println(((Student)it.next()).name);
}
}
}
class Student
{
public String name;
public int num;
public Student(String name,int num)
{
this.name = name;
this.num = num;
}
}
复制代码
作者:
crazystraw
时间:
2014-6-1 09:34
为什么我看代码时没有反应过来是缺泛型呢……继续努力
作者:
GoodBoy123
时间:
2014-6-1 09:43
hashSet.png
(153.02 KB, 下载次数: 12)
下载附件
2014-6-1 09:43 上传
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2