java 使用 ArrayList 作为值填充 HashTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13038880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:17:33  来源:igfitidea点击:

Populating HashTable with ArrayList as a value

javaarraylisthashtable

提问by Shepard

I am having problems with creating HashTable in my program, I am creating WordLadder game. Basically I want my HashTable to contain key which is unique word and value would be words which are one letter difference. Problem is that when I print out to check what I am putting into it, it prints perfectly what I want, however when I return HashTable it returns me nonsense.

我在我的程序中创建 HashTable 时遇到问题,我正在创建 WordLadder 游戏。基本上,我希望我的 HashTable 包含唯一单词的键,值是一个字母差异的单词。问题是,当我打印出来检查我放入的内容时,它完美地打印了我想要的东西,但是当我返回 HashTable 时,它​​返回了我的废话。

My code for generating HashTable is following:

我生成 HashTable 的代码如下:

public Hashtable<String, ArrayList<String>> findNeighbors(){
      Hashtable<String, ArrayList<String>> data = new Hashtable<String, ArrayList<String>>();
      ArrayList<String> neighb = new ArrayList<String>();
      for(int i=0; i < 5; i++){
       for(int j=0; j < 5; j++){
        if (isNeighbor(words.get(i), words.get(j))) {
         neighb.add(words.get(j));
        }
       }
       data.put(words.get(i), neighb);
       //System.out.println(words.get(i)+ " "+data.get(words.get(i))); <This Works perfectly fine
       System.out.println(data.toString()); //<This returns nonsense
       neighb.clear();
      }

      return data;
     }

public boolean isNeighbor(String a, String b){
      int diff = 0;
      for (int i = 0; i < a.length(); i++){
       if(a.charAt(i) != b.charAt(i)){
        diff++;
       }
      }
      return diff==1;
     }

采纳答案by Yogendra Singh

Look at neighb.clear();, You are calling on the same object you put in your Hashtable. You are clearing your list soon after putting it in the Hashtable. May you want to create a new local arraylist, add all elements to the new arraylist add then add the new arraylist in Hashtable and then clear your neighbe.g.

neighb.clear();,您正在调用放入哈希表的同一个对象。将列表放入哈希表后,您很快就会清除列表。可能您想创建一个新的本地数组列表,将所有元素添加到新的数组列表中 add 然后在 Hashtable 中添加新的数组列表,然后清除您的neighbeg

      List<String> newList = new ArrayList<String>();
      newList.addAll(neighb);
      data.put(words.get(i), newList);
      newList.clear();

回答by Amit Deshpande

You can not use same reference to put inside HashTablefor all keys. Each time you call clear()it clears list that you have added which is same for all keys. So create ArrayList for each key

您不能使用相同的引用来放置HashTable所有键。每次调用clear()它都会清除您添加的所有键相同的列表。So create ArrayList for each key

  for(int i=0; i < 5; i++){
  ArrayList<String> neighb = new ArrayList<String>();<-- Move inside loop
   for(int j=0; j < 5; j++){
    if (isNeighbor(words.get(i), words.get(j))) {
     neighb.add(words.get(j));
    }
   }
   data.put(words.get(i), neighb);
   //System.out.println(words.get(i)+ " "+data.get(words.get(i))); <This Works perfectly fine
   System.out.println(data.toString()); //<This returns nonsense

  }