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

同時提供兩個列表內容的迭代器?

Provide an iterator over the contents of two lists simultaneously?(同時提供兩個列表內容的迭代器?)
本文介紹了同時提供兩個列表內容的迭代器?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

假設我有這個:

public class Unit<MobileSuit, Pilot> {

    ...

    List<MobileSuit> mobileSuits;
    List<Pilot> pilots;

    ...
}

并且我想在該類之外以最簡單的方式遍歷這對.我該怎么做呢?我想過這樣做:

And I would like to iterate through the pair of each in the simplest way outside of that class. How should I go about doing that? I thought about doing this:

public class Unit<MobileSuit, Pilot> {

    ...
    Iterator<MobileSuit> iteratinMechas;
    Iterator<Pilot> iteratinPeople;

    class IteratorCustom<MobileSuit, Pilot> implements Iterator {

        public boolean hasNext() {
            return iteratinMechas.hasNext() && iteratinPeople.hasNext();
        }

        public void remove() {
            iteratinMechas.remove();
            iteratinPeople.remove();
        }

        public Object next() {
            // /!
        }

    }

    public Iterator iterator() {
        return new IteratorCustom<MobileSuit, Pilot>(mobileSuits, pilots);
    }
}

類似的東西.

無論如何,問題是我不能真正從 next() 返回單個對象,而且我也不能讓迭代器采用多種類型.那么,有什么想法嗎?

Anyway, the problem is that I can't really return just a single object from next(), and I also can't have a Iterator take more than one type. So, any thoughts?

另外,我無法創建一個新課程來結合 MobileSuit 和 Pilot.我需要將它們分開,即使我一次遍歷兩者.原因是可能有沒有飛行員的機動戰士,我不確定如何通過將它們保持在同一級別來解決這個問題.這個類需要在其他地方處理,所以我必須圍繞它和很多其他東西統一一個接口.基本上,假設 MobileSuit 和 Pilot 需要分開.

Also, I can't make a new class to combine MobileSuit and Pilot. I need to keep them separate, even though I'm iterating through both at a time. The reason is that there might be Mobile Suits that have no pilots, and I'm not sure how to fix that by keeping them at the same class. This class needs to be processed in other places, so I'd have to unify a interface around that and a lot of other stuff. Basically, assume MobileSuit and Pilot need to be separated.

推薦答案

無論如何,問題是我不能真正從 next() 返回單個對象,而且我也不能讓迭代器采用多種類型.那么,有什么想法嗎?

Anyway, the problem is that I can't really return just a single object from next(), and I also can't have a Iterator take more than one type. So, any thoughts?

顯然,您將需要一個輕量級的pair"類.這大致類似于 Map.Entry 內部類.

Obviously you are going to need a light-weight "pair" class. This is roughly analogous to the Map.Entry inner class.

這是一個通用解決方案的粗略:

Here's a rough cut at a generic solution:

public class ParallelIterator <T1, T2> implements Iterator<Pair<T1, T2>> {

    public class Pair<TT1, TT2> {
        private final TT1 v1;
        private final TT2 v2;
        private Pair(TT1 v1, TT2 v2) { this.v1 = v1; this.v2 = v2; }
        ...
    }

    private final Iterator<T1> it1;
    private final Iterator<T2> it2;

    public ParallelIterator(Iterator<T1> it1, Iterator<T2> it2) { 
        this.it1 = it1; this.it2 = it2;
    }

    public boolean hasNext() { return it1.hasNext() && it2.hasNext(); }

    public Pair<T1, T2> next() {
        return new Pair<T1, T2>(it1.next(), it2.next());
    }

    ...

}

注意:這并沒有明確處理列表長度不同的情況.將會發生的情況是,較長列表末尾的額外元素將被靜默忽略.

Note: this doesn't explicitly deal with cases where the lists have different lengths. What will happen is that extra elements at the end of the longer list will be silently ignored.

這篇關于同時提供兩個列表內容的迭代器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
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)
主站蜘蛛池模板: 欧美日韩精品区 | 久久精品中文字幕 | 亚洲久久在线 | 色爽女| 特黄一级 | 亚洲精品成人av久久 | 日韩伦理一区二区 | 免费视频成人国产精品网站 | 99精品视频免费在线观看 | 狠狠操在线 | 亚洲狠狠 | 欧美成人精品二区三区99精品 | 丁香综合 | 毛片免费看| 欧美日韩在线精品 | 久久青青 | 一区二区福利视频 | 欧美中文一区 | 日韩成人在线免费视频 | 久草热线 | 偷拍亚洲色图 | 99re6在线视频 | 日韩精品亚洲专区在线观看 | 亚洲成人天堂 | 久久一区二区精品 | 紧缚调教一区二区三区视频 | 美女视频一区二区三区 | 久久久久久久久久久久91 | 亚洲成人av | 欧美一区免费 | 日本不卡一区 | 天天躁日日躁aaaa视频 | 国产视频第一页 | 久久久精品一区二区三区 | 国产成人免费视频 | 亚洲免费精品 | 一区二区三区在线观看免费视频 | eeuss国产一区二区三区四区 | 中文字幕免费视频 | 亚洲精品一| 国产一区二区不卡 |