【发布时间】:2013-12-13 14:28:56
【问题描述】:
所以我正在使用散列并希望创建一个 LinkedList 数组。如何使用 table[index] 将新对象添加到 LinkedList。这是我到目前为止所拥有的,但是当我尝试调用 LinkedList 方法时添加它不起作用。是因为一切都事先设置为空吗?我需要手动添加所有内容吗?
private void populateLinkedList(LinkedList<String>[] table, ArrayList<String> dictionary){
for(String s:dictionary){
String temp=findHash(s);
System.out.print(temp + ": ");
int hashKey=hashFunction(temp);
Anagram obj=new Anagram(s, temp, hashKey);
table[hashKey].add(obj);
}
}
populateLinkedList(hashTable, dictionaryList);这就是我调用函数的方式。
【问题讨论】:
-
“是不是因为所有东西都事先设置为空?我需要手动添加所有东西吗?” ...可能。
-
...when I try to call the LinkedList method add it doesn't work.你是否得到某种类型的异常或者它对“不起作用”有什么作用? -
hashTable的类型是什么?无法实例化LinkedList<String>[],因此无法向该方法传递正确的参数。 -
你收到
ArrayStoreException了吗? -
@MattBall 您的评论非常有意义,OP 在方法之外声明了
LinkedList<String>[] arr = null;。
标签: java arrays hash linked-list populate