给定以下代码,如何迭代类型ProfileCollection的对象?
public class ProfileCollection implements Iterable {
private ArrayList m_Profiles;
public Iterator iterator() {
Iterator iprof = m_Profiles.iterator();
return iprof;
}
...
public Profile GetActiveProfile() {
return (Profile)m_Profiles.get(m_ActiveProfile);
}
}
public static void main(String[] args) {
m_PC = new ProfileCollection("profiles.xml");
// properly outputs a profile:
System.out.println(m_PC.GetActiveProfile());
// not actually outputting any profiles:
for(Iterator i = m_PC.iterator();i.hasNext();) {
System.out.println(i.next());
}
// how I actually want this to work, but won't even compile:
for(Profile prof: m_PC) {
System.out.println(prof);
}
}
最后
以上就是玩命秋天最近收集整理的关于java iterable 使用_java – 如何实现Iterable接口?的全部内容,更多相关java内容请搜索靠谱客的其他文章。
发表评论 取消回复