简体   繁体   English

hashtable类中的方法需要返回枚举类型

[英]Method in hashtable class needs to return Enumeration type

I have searched and can't seem to find an answer on this.我已经搜索过,似乎无法找到答案。 I understand what an Enumeration type is, but I can't for the life of me determine how to create and return one in a function.我了解枚举类型是什么,但我终生无法确定如何在函数中创建和返回枚举类型。 Basically, I have a function:基本上,我有一个功能:

public Enumeration getKeys(){
    //Returns an Enumeration of valid keys in the hash table
}

that needs to return an Enumeration.需要返回一个枚举。

My hashtable contains a key object and a value object, and I have already implemented methods like containsKey(Object key) and retrieve(Object key) to help me determine if a key is valid in the table and what the contents of the table at the key value are.我的哈希表包含一个键对象和一个值对象,我已经实现了 containsKey(Object key) 和retrieve(Object key) 之类的方法来帮助我确定表中的键是否有效以及表中的内容是什么关键值是。 Any help on understanding where to start with building an Enumeration type and returning it would be greatly appreciated.任何有关了解从哪里开始构建枚举类型并返回它的帮助将不胜感激。

It is "relatively" easy, change your code like this:它“相对”容易,像这样改变你的代码:

public Enumeration getKeys(){
       return new Enumeration() {

        @Override
        public boolean hasMoreElements() {
            // TODO Test if this enumeration contains more elements.
            return false;
        }

        @Override
        public Object nextElement() {
            // TODO Return the next element of this enumeration if this enumeration object has at least one more element to provide.
            return null;
        }
    };
    }

Now it is up to you to implement the two methods with respect of you business rules.现在由您来根据您的业务规则来实现这两种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM