gpt4 book ai didi

java - head 无法解析或不是字段错误

转载 作者:行者123 更新时间:2023-12-02 11:38:50 24 4
gpt4 key购买 nike

get() 方法将配置作为输入,如果该配置已位于哈希表中,则返回该配置的记录。我定义的变量和方法如下:

/*** Variables ***/
private LinearNode<TTTRecord> head = null;
private LinearNode<TTTRecord> current;
private LinearNode<TTTRecord> Hashnode;
private LinkedList list;
private LinkedList[] hashtable;
private int numElements;
private int index;
private String[] configArray;

public void remove(String config) throws InexistentKeyException {
index = hashfunction(config);

current = hashtable[index].head;

if (hashtable[index].isEmpty())
throw new InexistentKeyException(config);

if (current.getElement().getConfiguration().equals(config)) {
hashtable[index].remove(current.getElement());
return;
}
while ((current.getNext() != null) && (current.getNext().getElement() != null)) {
if (current.getElement().getConfiguration().equals(config)) {
hashtable[index].remove(current.getElement());
return;
}
current = current.getNext();
}
if (current.getElement() == null && current.getNext() == null)
throw new InexistentKeyException(config);

}

我不确定为什么行 current = hashtable[index].head; 显示错误 head 无法解析或不是字段 并且我没有知道如何修复它。

最佳答案

你的哈希表是一个链表数组。

当你执行hashtable[index]时,这会给你一个linkedList。现在要获取链接列表中的第一个元素,请使用 LinkedList::getFirst

hashtable[index].getFirst();

如果链接列表本身存储有用的内容(例如自定义类型),则需要在字段声明中指定,否则 Java 将假定它们包含任何内容并将它们全部视为对象

private LinkedList<MyType>[] hashTable;

然后您可以执行 current = hashtable[index].getFirst().head; (假设 MyType 有一个名为 head 的可访问字段)。

关于java - head 无法解析或不是字段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48725574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com