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

时间:2015-10-29 21:11:15

标签: java parameters

我有这段代码:

    @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;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

candidateslotNumber变量的声明只在包含它们的最里面的块中可见,在这种情况下,它不包含任何其他内容。看起来你想将使用这些变量的代码移动到该块中(而不是将声明移出它):

    @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);
            }
        }
    }

答案 1 :(得分:0)

您无法在这些位置进行比较 - 或者根本无法访问它们 - 因为它们超出了范围。你已经在&#39; else&#39;中声明了它们。你的if语句的一部分,这是他们的范围。

所以,解决方案是要么将声明移到if之外,要么将你的代码移到#else;&#39; else&#39;代码块。你选择哪个取决于你是否想要访问&#39; slotNumber&#39;或者候选人&#39;随后,如果没有,那么我个人更喜欢第二个,所以:

    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);
        }
    }