【发布时间】:2016-08-06 03:49:21
【问题描述】:
最初我一直在使用这样的数组列表:
ArrayList<JavaBean> AllBeans = new ArrayList<JavaBean>();
我可以遍历它并让它像这样打印我所有的 bean:
for (int i=0;i<AllBeans.size();i++){
System.out.println(AllBeans.get(i).getCost());
}
但我发现我应该这样做:
Collection<JavaBean> AllBeans = new ArrayList<JavaBean>();
但如果我这样做了,我就不能再做 AllBeans.get(i).getCost(),so I read on stackoverflow, and I apparently need to user an iterator。但是,我不明白我在做什么。我希望能够使用原始的 bean 类,但我无法使用它们?我只想从我的 bean 对象中使用我的 bean.getCost()。
我唯一能得到的是
Iterator itr = new AllBeans.Iterator();
while (itr.hasNext()){
System.out.println(itr.next().toString())
}
哪一个根本没有到达我的对象?我仍然无法理解 java 中的抽象和接口。
【问题讨论】:
-
什么是
woz?为什么你期望新的woz的迭代器不是空的? -
跑题了,但你的一句话让我很开心:_但是,我不明白我在做什么:
-
Iterator<JavaBean> itr = AllBeans.Iterator();. -
@AndyTurner 正在重命名这个问题的变量以使其更有意义,但忘记重命名那个。将其修复为 AllBeans
标签: java