【问题标题】:Error while creating generic hash table创建通用哈希表时出错
【发布时间】:2013-06-25 18:51:57
【问题描述】:

我在尝试创建通用 HashTable 时遇到以下错误。

The method put(String, capture#2-of ?) in the type Hashtable<String,capture#2-of ?> is not applicable for the arguments (String, int)

我需要一个 HashTable,如下所示: HashTable<String, HashTable<String, AnyWrapperType>>

by AnyWrapperType i mean it could be Integer or String or Character or Boolean

我将? 用于AnyWrapperType,但出现错误。

我还关注了link,从那里我知道自己做错了什么,但可以找到出路。

    Hashtable<String, Hashtable<String, String>> paramTocol = new Hashtable<>();

    if (standard != null && (!standard.isEmpty())) {
        Hashtable<String, String> temp = new Hashtable<>();
        temp.put(Appconstants.Col_studentinfo_S_Class,
                AvailableClass.getValueByName(standard));
        paramTocol.put("standard", temp);
    }
    if (section != null && (!section.isEmpty())) {
        Hashtable<String, String> temp = new Hashtable<>();
        temp.put(Appconstants.Col_studentinfo_S_Section, section);

        paramTocol.put("section", temp);
    }

这里standard is Integer在数据库中的实际值和section is char对应的值。我将所有这些值作为字符串给出,我需要将它们存储在 HashTable 中,所以我想要一些原始的哈希表来保存所有这些值。没有一个类型是用户定义的

【问题讨论】:

  • 不清楚你现有的代码是什么样的,或者你之后试图对表格做什么(或者你为什么不使用HashMap
  • 我添加了更多描述,希望能解决我的问题
  • 并非如此——我们不知道所涉及的任何类型。例如Appconstants.Col_studentinfo_S_Section 是什么?
  • Appconstants.Col_studentinfo_S_Section 是一个字符串,对应于数据库中的列名。
  • 所以您的示例代码中的所有内容都是关于字符串的——包装器类型从何而来?如果您展示一个简短但 完整 程序(控制台应用程序),它实际上可以证明您的问题,那将非常有帮助。请阅读tinyurl.com/so-hints

标签: java collections hashtable


【解决方案1】:

我认为您可以将AnyWrapperType 设置为Object。由于 java 中的所有类都是 Object 的直接或间接子类,因此您可以将类型设置为 Object,而不是设置泛型类型。 当您需要获取该元素时,您应该执行type-cast

HashTable<String, HashTable<String, Object>>

【讨论】:

    猜你喜欢
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 2012-01-02
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    相关资源
    最近更新 更多