gpt4 book ai didi

java - 参数化对象无法解析为变量

转载 作者:行者123 更新时间:2023-12-01 10:59:56 25 4
gpt4 key购买 nike

我有这段代码:

    @SuppressWarnings("unchecked")
public void put(K key,V value){
if(this.containsKey(key)){
TableEntry<K,V> foundKey = (TableEntry<K,V>)this.getTableEntry(key);
foundKey.setValue(value);
} else{
int slotNumber = Math.abs(key.hashCode()) % size;
TableEntry<K,V> candidate = (TableEntry<K,V>) elements [slotNumber];
}

// empty slot
if(candidate == null){
elements[slotNumber] = new TableEntry(key,value,null);
}else{
while(candidate != null){
candidate = candidate.next;
}
candidate.next = new TableEntry(key,value,null);
}
}

变量 candidateslotNumber在 Eclipse 中带有下划线,TableEntry() 的调用也是如此。构造函数。你能告诉我为什么我不能将候选者与 null 进行比较吗?

如果您需要,这里是完整的类(哈希表):

package hr.fer.oop.lab3.prob2;

public class SimpleHashtable<K,V> {

private V[] elements;
private static int defaultsize = 16;
private int size;

public SimpleHashtable(){
this(defaultsize);
}

@SuppressWarnings("unchecked")
public SimpleHashtable(int initialCapacity){
if(initialCapacity < 1) {
throw new IllegalArgumentException("Capacity must be at least 1.");
}
elements = (V[])new Object[calculateCapacity(initialCapacity)];
}

public int calculateCapacity(int number){
int result = 2;
while(result < number){
result = result << 1;
}
System.out.println(result);
return result;
}

@SuppressWarnings("unchecked")
public void put(K key,V value){
if(this.containsKey(key)){
TableEntry<K,V> foundKey = (TableEntry<K,V>)this.getTableEntry(key);
foundKey.setValue(value);
} else{
int slotNumber = Math.abs(key.hashCode()) % size;
TableEntry<K,V> candidate = (TableEntry<K,V>) elements [slotNumber];
}

// empty slot
if(candidate == null){
elements[slotNumber] = new TableEntry(key,value,null);
}else{
while(candidate != null){
candidate = candidate.next;
}
candidate.next = new TableEntry(key,value,null);
}
}

@SuppressWarnings("unchecked")
private TableEntry<K,V> getTableEntry(K key) {
int slotNumber = Math.abs(key.hashCode()) % this.size;
TableEntry<K,V> candidate = (TableEntry<K,V>) elements [slotNumber];

while(candidate != null){
if(key.equals(candidate.getKey())){
return candidate;
}
candidate = candidate.next;
}
return null;
}

private boolean containsKey(K key) {
return false;
}

private static class TableEntry<K,V>{
K key;
V value;
TableEntry next = null;

public TableEntry(K key, V value, TableEntry next){
this.key = key;
this.value = value;
this.next = next;
}

K getKey(){
return key;
}

V getValue(){
return value;
}

void setValue(V value){
this.value = value;
}

@Override
public String toString(){
return "Key:" + (String)key + "Value:" + (String)value;
}
}
}

最佳答案

您对 candidateslotNumber 变量的声明仅在包含它们的最内层 block 中可见,在本例中,该 block 不包含任何其他内容。看起来您想要将使用这些变量的代码移到该 block 中(而不是将声明移出该 block ):

    @SuppressWarnings("unchecked")
public void put(K key, V value){
if (this.containsKey(key)) {
TableEntry<K, V> foundKey = this.getTableEntry(key);
foundKey.setValue(value);
} else {
int slotNumber = Math.abs(key.hashCode()) % size;
TableEntry<K,V> candidate = (TableEntry<K, V>) elements[slotNumber];

// empty slot
if (candidate == null) {
elements[slotNumber] = new TableEntry<K, V>(key, value, null);
} else {
while (candidate != null) {
candidate = candidate.next;
}
candidate.next = new TableEntry<K, V>(key, value, null);
}
}
}

关于java - 参数化对象无法解析为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33424681/

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