链式哈希程序;该方法未定义类型错误

Chained Hashing Program; the method is undefined for the type error

我做了很多方法,都报错

The method add(int, ChainHashEx.Data) is undefined for the type ChainHash<Integer,ChainHashEx.Data>

还有一个问题就是

The constructor ChainHash<Integer,ChainHashEx.Data>(int) is undefined

为什么方法未定义? class 有问题吗?

还是我不知道的地方有问题

添加代码和 class 下面的方法。

代码 ChainHashEx :

package week14;

import java.util.Scanner;

public class ChainHashEx {
    static Scanner sc = new Scanner(System.in);
    
    static class Data {
        private int no;
        private String name;
        
        public int keyCode()    {
            return no;
        }
        
        public String toString() {
            return name;
        }
        
        void scanData(String guide, int sw) {
            System.out.println(guide + "enter data to add");
            if (sw == 1) {
                System.out.print("number : ");
                no = sc.nextInt();
                System.out.print("name : ");
                name = sc.next(); 
            } else {
                System.out.print("number : ");
                no = sc.nextInt();
            }
        }
    }
    
    static  void printMenu() {
        System.out.println("1. add 2. delete 3. search 4. print 5. exit ");
    }
    
    public static void main(String[] args) {
        int menu;
        Data data;
        Data temp = new Data();
        
        ChainHash<Integer, Data> hash = new ChainHash<Integer, Data>(13);
         
        do {
            printMenu();
            System.out.print("select menu: ");
            switch(menu = sc.nextInt()) {
            case 1 :
                data = new Data();
                data.scanData("add", 1);
                hash.add(data.keyCode(), data);
                break;
            case 2 : 
                temp.scanData("delete", 2);
                hash.remove(temp.keyCode());
                break;
            case 3 : 
                temp.scanData("search", 2);
                Data t = hash.search(temp.keyCode());
                if (t != null)
                    System.out.println("searched : " + t);
                else
                    System.out.println("the data does not exist");
                break;
            case 4 : 
                hash.dump();
                break;          
            }           
        } while (menu != 5);
        
        System.out.println("stop program");
    }
}

class 链哈希:

package week14;


public class ChainHash<K,V> {
    class Node<K, V> {
        private K key;
        private V data;
        private Node<K, V> next;


        public Node(K key, V data, Node<K, V> next) {
            this.key = key;
            this.data = data; 
            this.next = next;
        }
        
        K getKey() {
            return key;
        }   
        
        V getValue() {
            return data;
        }
        
        private int size;
        private Node<K,V>[] table;
        
        public void ChainHash(int capacity) { 
            try {
                table = new Node[capacity];
                this.size = capacity; 
            } catch (OutOfMemoryError e) {
                this.size = 0;
            }
        }
        
        public int hashValue(Object key) {
            return key.hashCode() % size;       
        }
        
        public V search(K key)  {
            int hash = hashValue(key);
            Node<K,V> p = table[hash];
            
            while (p != null) {
                if (p.getKey().equals(key))
                    return p.getValue();
            
                p = p.next;
            }
        
            return null;    
        
        }
        
        public int add(K key, V data)   {
            int hash = hashValue(key);
            Node<K,V> p = table[hash];
            
            while (p != null) {
                if (p.getKey().equals(key))
                    return 1;
            
                p = p.next;
            }
        
            Node<K,V> temp = new Node<K,V>(key, data, table[hash]);
            table[hash] = temp;
            return 0; 
        }       
        
        public void dump() {
            for (int i=0; i<size; i++) {
                Node<K,V> p = table[i];
                System.out.printf("%02d ", i);
                
                while (p != null) {
                    System.out.printf("-> %s (%s) ", p.getKey(), p.getValue());
                    p = p.next;
                }
                
                System.out.println();
            }
        }
        
        public int remove(K key)    {
            int hash = hashValue(key);
            Node<K,V> p = table[hash];
            Node<K,V> pp = null;
            
            while (p != null) {
                if (p.getKey().equals(key)) {
                    if (pp == null)
                        table[hash] = p.next;
                    else 
                        pp.next = p.next;
                
                    return 0;
                }
            
                pp = p;
                p = p.next;
            }
            
            return 1;   
        }
    }   
}

您的 ChainHash 方法定义在 内部 Node class,这就是找不到它们的原因。

我认为这是一个“大括号”错误:

  • 之后添加一个}
      V getValue() {
          return data;
      }
    

    “结束”Node class;

  • 去掉ChainHash底部的一个};

  • ChainHash 构造函数中删除 void

在这些修复之后,代码应该可以编译。