2

第一部分是我从网上找到的一个示例,并对其进行了修改以快速尝试不同的解决方案。正如标题所述,我不知道如何拉出 WordHold 对象,以便在需要检索它时继续修改它。

import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Set;

public class HashtableDemo {

    public static void main(String args[]) {
        WordHold temp = new WordHold("test1", "test2");
        WordHold temp2;
        // Creating Hashtable for example
        Hashtable companies = new Hashtable();

        // Java Hashtable example to put object into Hashtable
        // put(key, value) is used to insert object into map
        companies.put("Google", temp);
        companies.put("Nokia", "Finland"); 
        companies.put("Sony", "Japan");

        // Java Hashtable example to get Object from Hashtable
        // get(key) method is used to retrieve Objects from Hashtable
        temp2 = companies.get("Google");

        // Hashtable containsKey Example
        // Use containsKey(Object) method to check if an Object exits as key in
        // hashtable
        System.out.println("Does hashtable contains Google as key: "
            + companies.containsKey("Google"));

        // Hashtable containsValue Example
        // just like containsKey(), containsValue returns true if hashtable
        // contains specified object as value
        System.out.println("Does hashtable contains Japan as value: "
            + companies.containsValue("Japan"));

        // Hashtable enumeration Example
        // hashtabl.elements() return enumeration of all hashtable values
        Enumeration enumeration = companies.elements();

        while (enumeration.hasMoreElements()) {
            System.out
            .println("hashtable values: " + enumeration.nextElement());
        }

        // How to check if Hashtable is empty in Java
        // use isEmpty method of hashtable to check emptiness of hashtable in
        // Java
        System.out.println("Is companies hashtable empty: "
            + companies.isEmpty());

        // How to find size of Hashtable in Java
        // use hashtable.size() method to find size of hashtable in Java
        System.out.println("Size of hashtable in Java: " + companies.size());

        // How to get all values form hashtable in Java
        // you can use keySet() method to get a Set of all the keys of hashtable
        // in Java
        Set hashtableKeys = companies.keySet();

        // you can also get enumeration of all keys by using method keys()
        Enumeration hashtableKeysEnum = companies.keys();

        // How to get all keys from hashtable in Java
        // There are two ways to get all values form hashtalbe first by using
        // Enumeration and second getting values ad Collection

        Enumeration hashtableValuesEnum = companies.elements();

        Collection hashtableValues = companies.values();

        // Hashtable clear example
        // by using clear() we can reuse an existing hashtable, it clears all
        // mappings.
        companies.clear();
    }
}

正在使用的子类。

public class WordHold
{
    // instance variables - replace the example below with your own
    private String wrongWord;
    private String correctWord;

    private WordHold next;

    public WordHold(String wordWrong, String wordCorrect)
    {
        wrongWord = wordWrong;
        correctWord = wordCorrect;
    }

    /**
     * Returns the wrong word
     * 
     * @return     The stored wrong word.
     */
    public String getWrongWord()
    {
        return wrongWord;
    }

    /**
     * Returns the correct word
     * 
     * @return     The stored wrong word.
     */
    public String getCorrectWord()
    {
        return correctWord;
    }

    /**
     * Returns the next element
     * 
     * @return     The next element in list.
     */
    public WordHold getNext()
    {
        return next;
    }

    /**
     * Sets next element in the list.
     */
    public WordHold setNext()
    {
        return next;
    }

    public String toString()
    {
        String temp = (wrongWord + " no " + correctWord);

        return temp;
    }
}
4

3 回答 3

1

WordHold t = (WordHold)companies.get("Google");

将从 HashTable 公司检索它

temp2 = companies.get("Google");这一行不应该编译

于 2012-12-08T05:25:56.433 回答
1

我只浏览了您的代码,发现您没有在集合中使用泛型。我想,当您尝试将 WorldHold 对象从 Hashtable 中取出时,您必须将其强制转换为 WorldHold,就好像您不使用泛型一样,它会返回java.Lang.Object

像这样声明你的哈希表:

Hashtable<WordHold> companies = new Hashtable<WordHold>();

将解决问题。

或者如果您不想使用泛型,则必须将返回的对象显式转换为 WordHold。

    temp2 = (WordHold)companies.get("Google");

如果您使用的是 java 1.5+,我强烈建议您使用泛型。因为它们为您的集合添加了编译时安全性。如果您不使用它们,您可以简单地将任何对象添加到您的集合中,并且您可以在从集合中检索对象时摆脱这种不需要的转换。

在 java 中阅读泛型

于 2012-12-08T05:26:15.863 回答
0

希望这个例子对你有帮助

import java.util.*;

public class hashtable {

        public static void main(String[] args) {
                Hashtable hastab = new Hashtable();
                hastab.put("a", "andrews");
                hastab.put("b", "bob");
                hastab.put("c", "christina");
                hastab.put("d", "dude");
                hastab.put("e", "era");
                Set s = hastab.entrySet();
                Iterator it = s.iterator();

                while (it.hasNext()) {
                        System.out.println(it.next());
                }
        }
}

如果你想从一个简单的类中提取 System.out.println(it.next().value().getWordWrong())

(为你的 wordwrong 类中的两个字符串创建一个 getter)

于 2012-12-08T05:30:25.947 回答