久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何實現 Iterable 接口?

How can I implement the Iterable interface?(如何實現 Iterable 接口?)
本文介紹了如何實現 Iterable 接口?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

鑒于以下代碼,我如何迭代 ProfileCollection 類型的對象?

Given the following code, how can I iterate over an object of type 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);
     }
}

推薦答案

Iterable 是一個泛型接口.您可能遇到的一個問題(您實際上并沒有說您遇到了什么問題,如果有的話)是,如果您使用泛型接口/類而不指定類型參數,您可以刪除不相關的泛型類型的類型班內.這方面的一個例子是 對泛型類的非泛型引用導致非泛型返回類型.

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.

所以我至少會把它改成:

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);
    }
}

這應該有效:

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

如果沒有 Iterable 上的類型參數,迭代器可能會被簡化為 Object 類型,所以只有這樣才能工作:

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

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

這是 Java 泛型的一個非常晦澀的極端案例.

This is a pretty obscure corner case of Java generics.

如果沒有,請提供更多關于發生了什么的信息.

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

這篇關于如何實現 Iterable 接口?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
egit plugin for Eclipse(Eclipse 的 egit 插件)
Gitlab cannot open git-upload-pack error(Gitlab 無法打開 git-upload-pack 錯誤)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
How to add GitLab repository in Eclipse?(如何在 Eclipse 中添加 GitLab 存儲庫?)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
主站蜘蛛池模板: 天天碰夜夜操 | 久久精品国产免费一区二区三区 | 玖玖综合在线 | 亚洲成人av一区二区 | 国产一区二区三区色淫影院 | 亚av在线| 欧美激情久久久 | 亚洲欧美国产精品久久 | 精品一区二区三区免费毛片 | 久久不卡区 | 一区二区三区四区日韩 | 国产露脸国语对白在线 | 久久综合狠狠综合久久综合88 | 99热在这里只有精品 | 欧美天堂在线 | 亚洲欧美日韩精品久久亚洲区 | 久久精品| 国产精品美女久久久久aⅴ国产馆 | 欧美一二区 | 色橹橹欧美在线观看视频高清 | 毛片a | 久久久综合 | 自拍视频在线观看 | 久久精品亚洲国产 | 玖草资源 | 一级午夜aaa免费看三区 | 一区二区三区中文字幕 | 日韩中文字幕视频在线观看 | 亚洲一区免费 | 欧美精品一区二区三区四区五区 | 精品成人69xx.xyz | 亚洲成人免费视频在线观看 | 人成精品 | 毛片免费观看 | 精品一区二区三区四区 | 69av在线视频 | 男女免费视频网站 | 欧美日日日日bbbbb视频 | 黑人巨大精品欧美一区二区免费 | 欧美日韩久| 99这里只有精品视频 |