简体   繁体   English

遍历哈希表并使用get和containsKey方法

[英]Iterating through hashtable and using the get and containsKey methods

Having trouble with the containsKey and get method, also not sure how to iterate through the hashtable keys and values, I want to iterate through and add the keys with the value of true to the solution list 在遇到containsKey和get方法时遇到麻烦,也不确定如何遍历哈希表的键和值,我想遍历并将键值为true的键添加到解决方案列表中

1st error: The method containsKey(int) is undefined for the type hashTable 第一个错误:类型hashTable的方法containsKey(int)未定义

2nd error: Can only iterate over an array or an instance of java.lang.Iterable 第二个错误:只能迭代数组或java.lang.Iterable的实例

3rd error: The method get(int) is undefined for the type hashTable 第三个错误:类型为hashTable的方法get(int)未定义

  package practice;

        import java.util.*;
        /*

        You are given an integer array, where all numbers except for TWO numbers appear even number of times. 

        Q: Find out the two numbers which appear odd number of times.

        */

        public class hashTable{
        public static void main(String[] args){
            int[] test = {2, 2, 5, 7, 4, 4};
            List<Integer> solution = new ArrayList<Integer>();
            Hashtable<Integer, Boolean> ht = new Hashtable<Integer, Boolean>(); 
            Boolean check = true;
            for (int item : test){
//error             if (!containsKey(item)){
                    check = true;
                } else{
                    check = false;
                    ht.put(item, check);

                }
                }

//error            for (int item : ht){
//error             if (get(item) == true){
                    solution.add(item);
                }
                }
            System.out.println("the result is");
            System.out.println(solution);
            }

         }

The issue would seem that you are using a generic type without providing the generic information. 问题似乎是您使用的是通用类型,而没有提供通用信息。 If you put @SuppressWarnings("unchecked") (or something along those lines, do not have an IDE to test), the warning should go away. 如果放置@SuppressWarnings("unchecked") (或类似的内容,没有要测试的IDE),则警告应消失。 That being said, I would advise against that. 话虽如此,我建议不要这样做。

Ideally, you should provide generic information and do as @BackSlash suggested: Hashtable<Integer, String> ht = new Hashtable<Integer, String>(); 理想情况下,您应该提供常规信息并按照@BackSlash的建议进行操作: Hashtable<Integer, String> ht = new Hashtable<Integer, String>(); . This will provide the compiler with the generic information it needs and makes your collection type safe which is something you usually want. 这将为编译器提供所需的一般信息,并使您的集合类型安全 ,这是您通常需要的。

That being said, I would recommend a different approach: 话虽这么说,我会建议一种不同的方法:

  1. Iterate over your array. 遍历数组。
  2. Create a hash table: HashTable<Integer, Boolean> . 创建一个哈希表: HashTable<Integer, Boolean>
  3. If the number you are iterating on exists in the hash table, then apply the not operator on the value at HashTable[number] . 如果哈希表中存在要迭代的数字,则对HashTable[number]的值应用not运算符。
  4. If not, add a new entry and set the boolean value to false. 如果不是,则添加一个新条目并将布尔值设置为false。

Seeing your comment, you could try something like so: 看到您的评论,您可以尝试如下操作:

Hashtable<Integer, Boolean>  table = new Hashtable<Integer, Boolean>();
int[] nums = ...
for(Integer number : nums)
{
    if(table.containsKey(number))
    {
        table.put(number, !table.get(number));
    }
    else
    {
        table.put(number, false);
    }
}

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

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