同时提供两个列表的内容的迭代器? [英] Provide an iterator over the contents of two lists simultaneously?

查看:112
本文介绍了同时提供两个列表的内容的迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个:

  public class Unit< MobileSuit,Pilot> {

...

List< MobileSuit> mobileSuits;
列表< Pilot>飞行员;

...
}

我想在该类之外以最简单的方式迭代每一对。我应该怎么做呢?我想过这样做:

  public class Unit< MobileSuit,Pilot> {

...
迭代器< MobileSuit> iteratinMechas;
Iterator< Pilot> iteratinPeople;

class IteratorCustom< MobileSuit,Pilot>实现迭代器{

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

这些内容就是这样。



无论如何,问题是我不能真正从next()返回一个单独的对象,并且我也不能让Iterator采用多个类型。所以,有什么想法?

另外,我无法组建一个新课程来结合MobileSuit和Pilot。我需要让他们分开,即使我一次都在迭代。原因是可能有没有飞行员的移动套装,我不知道如何通过将他们保持在同一班级来解决这个问题。这个类需要在其他地方处理,所以我必须统一一个接口和许多其他的东西。基本上,假设MobileSuit和Pilot需要分开。

解决方案


无论如何,问题是我不能只从next()返回单个对象,而且我也不能让Iterator采用多种类型。所以,有什么想法?

显然你需要一个轻量级的对类。这大致类似于 Map.Entry 内部类。



这是对通用解决方案的粗略调整:

  public class ParallelIterator< T1,T2>实现Iterator< Pair< T1,T2>> {

public class Pair< TT1,TT2> {
private final TT1 v1;
private final TT2 v2;
私人对(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());
}

...

}

注意:这并不明确地处理列表具有不同长度的情况。会发生什么情况是,长列表末尾的额外元素将被默默忽略。


Suppose I have this:

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

Something along those lines.

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?

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.

解决方案

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?

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.

这篇关于同时提供两个列表的内容的迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆