如何创建一个类(这不是收集的后代)“兼容”与每个循环? [英] How to make a class (which is not a descendant of collection) "compatible" with for-each loop?

查看:118
本文介绍了如何创建一个类(这不是收集的后代)“兼容”与每个循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:在现实生活中,我会使用一个合适的Java 集合,但是在这个任务中,我想尽一切办法从头开始。 / p>

我已经在网上搜索了这里,在网上其他地方,但没有找到我正在寻找的东西。



我的理解是, for-each 循环可以在任何实现 iterable 接口,同时这个类<​​strong> not 必须实现 iterator 。我是否在这里?



比方说,我有以下两个类没有明确从任何其他类派生。

  public class Pile {

private Thing aThing = new Thing();

//其他变量
//构造函数
//其他方法(包括getters和setter)
}



 类Thing {

私有对象值; // Payload
private Thing link1; //启用感知的变量
private Thing link2; //其他Thing对象
//例如,link1可以引用前一个对象
//和link2 - 到下一个

//其他变量
//构造函数
//其他方法(包括getters和setter)

code


$ b

在这个例子中, Pile 应该是一个双链表List 。但是并不是必须的。



我的目标是通过继承创建 IterablePile 类。

  public class IterablePile extends Pile {
}



Iterable
接口的唯一要求是实现 Iterator 方法。 p>

在这里,我很难过。看来,所有的例子(或者至少是我目前发现的例子)都立即假定我的类是从Java 集合之一(例如的ArrayList )。

如果不是这样的话?在这种情况下究竟需要做什么?需要采取哪些措施?



你可以指出我正确的方向吗(最好不要写代码本身)?

题。如果 Thing 私有内部类 Pile $ b


$ b

在我看来,我缺少一些基本的东西,但不能把我的手指放在它上面。如果只有你的 IterablePile 需要被迭代,那么你只需要实现 Iterable 接口将提供一个 Iterator 。下面是一个基本的例子:

pre $ public class IterablePile extends Pile implements Iterable< Thing> {
//当前类实现...
private class MyIterablePileIterator实现Iterator< Thing> {
私人事物;
private MyIterablePileIterator(Thing thing){
this.thing = thing;

@Override
public boolean hasNext(){
//添加实现...
return(thing.getLink1()!= null || thing .getLink2()!= null);

@Override
public Thing next(){
//添加实现...
//因为它是一个树结构,所以可以使用队列<东西>
//实现前缀,中缀或后缀导航
}
@Override
public void remove(){
//添加实现...
//如果你不想实现它,你可以把它留空
//或抛出新的UnsupportedOperationException(永不删除!)
}
}
@Override
public Iterator< Thing> iterator(){
返回新的MyIterablePileIterator(getAThing());






$ b

不过,我会觉得很奇怪, code> IterablePile 是可迭代的,而 Pile 不会。请注意,您的代码应该面向接口(或抽象/超类),而不是特定的实现。无论如何,这应该做的。



更多信息:


NOTE: In real life situation I would use an appropriate Java collection but in this task I would like to do everything form scratch.

I've done my Googling here on SO and on the rest of the web, but didn't find exactly what I was looking for.

It's my understanding that for-each loop can operate on any class that implements iterable interface and at the same time this class does not have to implement iterator. Am I right here?

Let say, I have the following two classes that are not explicitly derived from any other class.

public class Pile {

   private Thing aThing = new Thing(); 

   // other varibles
   // constructor
   // other methods (including getters and setters)
}

and

public class Thing {

   private object value;   // Payload
   private Thing link1;    // variables that enable awareness
   private Thing link2;    // of other Thing objects
                           // For example, link1 could be reference to the previous object
                           // and link2 - to the next

   // other varibles
   // constructor
   // other methods (including getters and setters)
}

In this example, Pile would be a double-linked List. But it does not have to.

My goal would be to create IterablePile class through inheritance.

public class IterablePile extends Pile {
}

The only requirement of Iterable interface is to implement Iterator method.

And here I'm stumped. It seems that all examples (or at least those that I found so far) immediately assume that my class is derived from one of the Java collections (for example ArrayList).

What if this not the case? What exactly needs to be done in such case? What steps need to be taken?

Can you point me in the right direction (preferably not writing the code itself)?

And one more question. Will the situation change if Thing is a private inner class of Pile?

It seems to me that I'm missing something basic but can't put my finger on it.

解决方案

If only your IterablePile needs to be iterated, then you just have to implement the Iterable interface that will provide an Iterator. Here's a basic example:

public class IterablePile extends Pile implements Iterable<Thing> {
    //current class implementation...
    private class MyIterablePileIterator implements Iterator<Thing> {
        private Thing thing;
        private MyIterablePileIterator(Thing thing) {
            this.thing = thing;
        }
        @Override
        public boolean hasNext() {
            //add the implementation...
            return (thing.getLink1() != null || thing.getLink2() != null);
        }
        @Override
        public Thing next() {
            //add the implementation...
            //since it is a tree structure, you could use a Queue<Thing>
            //to implement prefix, infix or postfix navigation
        }
        @Override
        public void remove() {
            //add the implementation...
            //in case you don't want to implement it, you can leave it blank
            //or throw new UnsupportedOperationException("never remove!")
        }
    }
    @Override
    public Iterator<Thing> iterator() {
        return new MyIterablePileIterator(getAThing());
    }
}

Still, I would find very odd that only your IterablePile would be iterable, while the Pile won't. Note that your code should be oriented to interfaces (or abstract/super classes) instead of specific implementations. Anyway, this should do.

More info:

这篇关于如何创建一个类(这不是收集的后代)“兼容”与每个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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