Eclipse 说我没有实现枚举,但我实现了

发布于 2024-09-03 03:52:01 字数 1151 浏览 1 评论 0原文

大家早上好,

我在学校项目上遇到了一些问题。 我们被告知要创建一个迭代器来实现哈希映射上的枚举。

所以我做了这个枚举:

public Enumeration<Object> getEnumeration() {
        return new Enumeration<Object>() {
            private int itemsDone = 0;
            Collection<Long> keysCollection = getContent().keySet();            
            Long [] keys = keysCollection.toArray(new Long[keysCollection.size()]);


            @Override
            public boolean hasMoreElements() {
                if(itemsDone < getContent().size() +1 ) {
                    return true;
                }else {
                    return false;
                }

            }
            @Override
            public Object nextElement() {               
                return getContent().get(keys[itemsDone++]);
            }
        };
    }

这在我的 Backpack 类中

public class Backpack extends Item implements Carrier, Enumeration<Object>{

哈希图由 getContent() 返回。现在的问题是 eclipse 一直告诉我我还没有实现枚举中的方法。如果我使用快速修复,它只会在我的类中添加 hasMoreElements() 和 nextElement() 虚拟方法。 不知怎的,它在内部类中看不到这些方法。

任何人都可以帮助我吗?任何帮助表示赞赏。

Goodmorning everybody,

I'm having a little problem with a project for school.
We are told to make an Iterator that implements Enumeration over a Hashmap.

So i made this Enumeration:

public Enumeration<Object> getEnumeration() {
        return new Enumeration<Object>() {
            private int itemsDone = 0;
            Collection<Long> keysCollection = getContent().keySet();            
            Long [] keys = keysCollection.toArray(new Long[keysCollection.size()]);


            @Override
            public boolean hasMoreElements() {
                if(itemsDone < getContent().size() +1 ) {
                    return true;
                }else {
                    return false;
                }

            }
            @Override
            public Object nextElement() {               
                return getContent().get(keys[itemsDone++]);
            }
        };
    }

This goes in my Backpack class

public class Backpack extends Item implements Carrier, Enumeration<Object>{

The hashmap is returned by getContent(). The problem now is that eclipse keeps telling me I havent implemented the methods from Enumeration. If I use the quick fix it just adds the hasMoreElements() and nextElement() dummy methods in my class.
Somehow it doesn't see these methods in the inner class..

Can anyone help me please? Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄意 2024-09-10 03:52:01

Backpack 没有实现它。 getEnumeration 中使用的匿名内部类可以做到这一点。如果你想让Backpack本身实现Enumeration,它需要有这些方法。

Backpack doesn't implement it. The anonymous inner class used in getEnumeration does. If you want Backpack itself to implement Enumeration, it needs to have those methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文