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

在同一個類中實現(xiàn) Java Iterator 和 Iterable?

Implement Java Iterator and Iterable in same class?(在同一個類中實現(xiàn) Java Iterator 和 Iterable?)
本文介紹了在同一個類中實現(xiàn) Java Iterator 和 Iterable?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試?yán)斫?Java IteratorIterable 接口

I am trying to understand Java Iterator and Iterable interfaces

我正在寫這門課

class MyClass implements Iterable<String> {
    public String[] a = null;
    public MyClass(String[] arr) {
        a = arr;    
    }

    public MyClassIterator iterator() {
        return new MyClassIterator(this);
    }

    public class MyClassIterator implements Iterator<String> {
        private MyClass myclass = null;
        private int count = 0;
        public MyClassIterator(MyClass m) {
            myclass = m;    
        }

        public boolean hasNext() {
            return count < myclass.a.length;
        }
        public String next() {
            int t = count;
            count++;
            return myclass.a[t];
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }   
}

它似乎正在工作.

我應(yīng)該有:

Myclass implements Iterable<Stirng>, Iterator<String> {

}

或者我應(yīng)該把 MyClassIterator 放在 MyClass 之外

Or I should put MyClassIterator outside MyClass as

class MyClass implements Iterable<String> {
    public String[] a = null;
    public MyClass(String[] arr) {
        a = arr;    
    }
    public MyClassIterator iterator() {
        return new MyClassIterator(this);
    }
}


    public class MyClassIterator implements Iterator<String> {
        private MyClass myclass = null;
        private int count = 0;
        public MyClassIterator(MyClass m) {
            myclass = m;    
        }

        public boolean hasNext() {
            return count < myclass.a.length;
        }
        public String next() {
            int t = count;
            count++;
            return myclass.a[t];
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }   

哪個更好?

推薦答案

你應(yīng)該幾乎永遠(yuǎn)同時實現(xiàn) IterableIterator班級.他們做不同的事情.迭代器自然是有狀態(tài)的 - 當(dāng)您使用它進(jìn)行迭代時,它必須更新其對世界的看法.然而,一個可迭代對象只需要能夠創(chuàng)建新的迭代器.特別是,您可以讓多個迭代器同時處理同一個原始可迭代對象.

You should almost never implement both Iterable and Iterator in the same class. They do different things. An iterator is naturally stateful - as you iterate using it, it has to update its view of the world. An iterable, however, only needs to be able to create new iterators. In particular, you could have several iterators working over the same original iterable at the same time.

您當(dāng)前的方法非常好 - 我會更改實施的某些方面,但在職責(zé)分離方面很好.

Your current approach is pretty much okay - there are aspects of the implementation I'd change, but it's fine in terms of the separation of responsibilities.

這篇關(guān)于在同一個類中實現(xiàn) Java Iterator 和 Iterable?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 在线一级片 | 成人毛片在线观看 | 99精品视频在线 | 日韩视频精品在线 | 日韩成年人视频在线 | 99国内精品| 草樱av| 超碰人人艹| 91在线观看 | 国产视频一区在线 | 黄免费观看视频 | 国产一伦一伦一伦 | 成年无码av片在线 | 91素人| 欧美日韩在线一区二区三区 | 久久999| 国产成人精品高清久久 | 超碰日本| 好姑娘高清在线观看电影 | 一级黄色淫片 | 欧美自拍网站 | 7777精品伊人久久精品影视 | 欧美日韩亚洲三区 | 91福利在线观看视频 | 精品亚洲一区二区三区四区五区高 | 91porn成人精品 | 成人在线一区二区 | 亚洲精品免费观看 | 欧美二区在线 | 在线观看中文字幕 | 精品久久久久久亚洲精品 | 国产福利在线 | 欧美黑人又粗大 | 日本久久一区 | a看片 | 亚洲黄色国产 | 亚洲一区二区av | 亚洲精品乱码久久久久久按摩观 | 精品国产久 | 日本亚洲一区 | 激情国产在线 |