覆盖Iterables的正确方法< Obj>在Java中 [英] Correct Way to Override Iterables<Obj> in Java

查看:287
本文介绍了覆盖Iterables的正确方法< Obj>在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我为实现链表而编写的代码,

this is a code i have written for implementing linked list,

private class DequeIterator<Item> implements Iterable<Item> {
    private Node pElement;

    DequeIterator() {
        pElement = first;
    }

    public boolean hasNext() {
        return pElement != null;
    }

    public Item next() {
        if (!this.hasNext()) {
            throw new NoSuchElementException();
        }
        Item ret = pElement.it;
        pElement = pElement.next;
        return ret;
    }

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

我不知道接下来会出现什么问题错误请帮我解决这些问题

I dont know what is going wrong i am getting follwing errors please help me resolve those

=> Deque.java:25:错误:Deque.DequeIterator不是抽象的,并且不会覆盖Iterable中的抽象方法iterator()

=> Deque.java:25: error: Deque.DequeIterator is not abstract and does not override abstract method iterator() in Iterable

=> Deque.java:35:错误:不兼容的类型
Item ret = pElement.it;
^
必需:项目#2
找到:项目#1

=>Deque.java:35: error: incompatible types Item ret = pElement.it; ^ required: Item#2 found: Item#1

=> Deque.java:121:错误:不兼容的类型
返回新的DequeIterator();
^
必需:Iterator
找到:Deque.DequeIterator

=>Deque.java:121: error: incompatible types return new DequeIterator(); ^ required: Iterator found: Deque.DequeIterator

推荐答案

您正在实施错误的界面。 Iterable 要求您实现 iterator()方法,这是您未提供的方法。因此第一个错误。

You are implementing the wrong interface. Iterable requires that you implement the iterator() method, which you are not providing. Thus the first error.

你可能想要实现 Iterator ,需要 hasNext() next() remove()

You probably want to implement Iterator instead, which requires hasNext(), next() and remove().

你没有显示你的其余代码,但我认为你有 Deque.java ,其中包含其余的链表代码(即对<$ c的引用) $ c> head 等等,所以你可能希望 Deque使用迭代器实现Iterable< Item> ()返回 DequeIterator 实例的方法。

You didn't show the rest of your code, but I presume you have Deque.java which has the rest of the linked list code (i.e. a reference to the head etc.), so probably you might want to have Deque implements Iterable<Item> with the iterator() method returning an instance of DequeIterator.

您还需要从 DequeueIterator < Item> C $ C>。您已经说它实现了 Iterable< Item> ,因此您限制了类型。您正在将其更改为泛型类型,这是第二个错误的原因。我假设 Item 是在其他地方定义的具体类(特别是因为 pElement 返回它并且没有泛型类型与它相关联。)

You also need to remove the <Item> from the DequeueIterator. You are already saying it implements Iterable<Item>, so you are restricting the type. You are changing it to a generic type instead, which is the reason for the second error. I presume that Item is a concrete class defined somewhere else (especially since pElement returns it and does not have a generic type associated with it).

第三个错误,不知道121行中的内容,你没有包含它。但最有可能的原因与我上面提到的相同( DequeIterator 需要实现 Iterator 而不是 Iterable

The third error, no idea what's in line 121, you didn't include it. But most probably its for the same reason I mentioned above (DequeIterator needs to implement Iterator not Iterable)

这篇关于覆盖Iterables的正确方法&lt; Obj&gt;在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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