我是靠谱客的博主 玩命秋天,这篇文章主要介绍java iterable 使用_java – 如何实现Iterable接口?,现在分享给大家,希望可以做个参考。

给定以下代码,如何迭代类型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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(143)

评论列表共有 0 条评论

立即
投稿
返回
顶部