如何实现可迭代接口?

时间:2022-09-02 09:12:14

Given the following code, how can I iterate over an object of type ProfileCollection?

有了以下代码,我如何在类型ProfileCollection对象上进行迭代?

public class ProfileCollection implements Iterable {    
    private ArrayList<Profile> m_Profiles;

    public Iterator<Profile> iterator() {        
        Iterator<Profile> 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);
     }
}

2 个解决方案

#1


59  

Iterable is a generic interface. A problem you might be having (you haven't actually said what problem you're having, if any) is that if you use a generic interface/class without specifying the type argument(s) you can erase the types of unrelated generic types within the class. An example of this is in Non-generic reference to generic class results in non-generic return types.

Iterable是一个通用接口。您可能会遇到的一个问题(如果有的话,实际上您还没有说您遇到了什么问题)是,如果您使用通用接口/类而没有指定类型参数,那么您可以删除类中不相关的泛型类型的类型。一个例子是对泛型类的非泛型引用导致非泛型返回类型。

So I would at least change it to:

所以我至少要把它改成:

public class ProfileCollection implements Iterable<Profile> { 
    private ArrayList<Profile> m_Profiles;

    public Iterator<Profile> iterator() {        
        Iterator<Profile> iprof = m_Profiles.iterator();
        return iprof; 
    }

    ...

    public Profile GetActiveProfile() {
        return (Profile)m_Profiles.get(m_ActiveProfile);
    }
}

and this should work:

这应该工作:

for (Profile profile : m_PC) {
    // do stuff
}

Without the type argument on Iterable, the iterator may be reduced to being type Object so only this will work:

如果没有Iterable上的类型参数,迭代器可能会被简化为类型对象,因此只有以下方法可以工作:

for (Object profile : m_PC) {
    // do stuff
}

This is a pretty obscure corner case of Java generics.

这是Java泛型的一个非常模糊的情况。

If not, please provide some more info about what's going on.

如果没有,请提供更多的信息关于正在发生的事情。

#2


4  

First off:

首先:

public class ProfileCollection implements Iterable<Profile> {

Second:

第二:

return m_Profiles.get(m_ActiveProfile);

#1


59  

Iterable is a generic interface. A problem you might be having (you haven't actually said what problem you're having, if any) is that if you use a generic interface/class without specifying the type argument(s) you can erase the types of unrelated generic types within the class. An example of this is in Non-generic reference to generic class results in non-generic return types.

Iterable是一个通用接口。您可能会遇到的一个问题(如果有的话,实际上您还没有说您遇到了什么问题)是,如果您使用通用接口/类而没有指定类型参数,那么您可以删除类中不相关的泛型类型的类型。一个例子是对泛型类的非泛型引用导致非泛型返回类型。

So I would at least change it to:

所以我至少要把它改成:

public class ProfileCollection implements Iterable<Profile> { 
    private ArrayList<Profile> m_Profiles;

    public Iterator<Profile> iterator() {        
        Iterator<Profile> iprof = m_Profiles.iterator();
        return iprof; 
    }

    ...

    public Profile GetActiveProfile() {
        return (Profile)m_Profiles.get(m_ActiveProfile);
    }
}

and this should work:

这应该工作:

for (Profile profile : m_PC) {
    // do stuff
}

Without the type argument on Iterable, the iterator may be reduced to being type Object so only this will work:

如果没有Iterable上的类型参数,迭代器可能会被简化为类型对象,因此只有以下方法可以工作:

for (Object profile : m_PC) {
    // do stuff
}

This is a pretty obscure corner case of Java generics.

这是Java泛型的一个非常模糊的情况。

If not, please provide some more info about what's going on.

如果没有,请提供更多的信息关于正在发生的事情。

#2


4  

First off:

首先:

public class ProfileCollection implements Iterable<Profile> {

Second:

第二:

return m_Profiles.get(m_ActiveProfile);