当前位置: 首页 > 知识库问答 >
问题:

实现链表并存储文本文件的哈希表

弓磊
2023-03-14

我想使用链接和链表实现我自己的哈希表,但我很难弄清楚如何在main方法中使用实现。我需要读取一个逗号分隔的数据文件和存储名称作为键和两个浮点作为一个值。我知道我需要使用面向对象编程,但我有一个困难的时间访问我的数据使用我的实现。

下面是我的代码:public class LinkedListHash{

     String key;
     String value;
     LinkedListHash next;
    public LinkedListHash(){
    }
    LinkedListHash(String key, String value){
        this.key = key;
        this.value = value;
        this.next = null;
    }
    public String getValue(){
        return value;
    }
    public void setValue(String value){
        this.value = value;
    }
    public String getKey(){
        return key;
    }
    public LinkedListHash getNext(){
        return next;
    }
    public void setNext(LinkedListHash next){
        this.next = next;
    }

   class Hashtable {
    int size = 0;
    LinkedListHash[] table;
    Hashtable(){
        table = new LinkedListHash[size];
        for (int i = 0; i < size; i++){
            table[i] = null;
        }
    }
    public String get(String key){
        int hash = key.hashCode();
        if (table[hash] == null){
            return null;
        }
        else {
            LinkedListHash input = table[hash];
            while (input != null && input.getKey() != key){
                input = input.getNext();
            }
            if (input == null){
                return null;
            }
            else {
                return input.getValue();
            }
        }

    }
    public void put(String key, String value){
        int hash = key.hashCode();
        if (table[hash] == null){
            table[hash] = new LinkedListHash(key, value);
        }
        else {
            LinkedListHash input = table[hash];
            while (input.getNext() != null && input.getKey() != key){
                input = input.getNext();
            }
            if (input.getKey() == key){
                input.setValue(value);
            }
            else {
                input.setNext(new LinkedListHash(key, value));
            }
        }
    }

  }
}

 public static void main(String[] args) throws FileNotFoundException{

     Hashtable<String, String> tbl = new Hashtable<String, String>();

     String path = args[0];

     if(args.length < 1) {
            System.out.println("Error, usage: java ClassName inputfile");
        System.exit(1);
        }

        Scanner reader = new Scanner(new FileInputStream(args[0]));

        while((path = reader.nextLine()) != null){
            String parts[] = path.split("\t");

            tbl.put(parts[0], parts[1]);
         } reader.close();

 }  }

任何改进代码的方法都是有帮助的。请记住,我不是一个非常有经验的程序员,所以我为任何可怕的错误道歉。

共有1个答案

赵灼光
2023-03-14

字符串%s不应与==!=进行比较,除非您想知道它们是否是占用相同内存位置的相同字符串。改用equals()

 类似资料:
  • 我有一个项目,我需要在Java创建一个内存管理程序,我有文本文件,但不知道从哪里开始。我已经熟悉了Java中的方法,但基本上我希望索引作为键,后面的一切作为值。 这是文本文件,因为它很大AF:https://pastebin.com/Q4whQHxW 代码:

  • 我正在尝试使用 fwrite 将哈希表写入 C 文件并使用 fread 读取它。我有这个结构: 当我使用恐惧时,我有 和 fwrite: 但是,我无法在读写后正确访问数据。问题是什么?

  • 问题内容: 我想制作一个将读取文本文件并存储每个出现的字符的Java程序。因此它将考虑标点符号,字母,数字,大写,小写等。给定一个文本文件,如: 玫瑰是红色的, 紫罗兰是蓝色的。 打印值将如下所示: R:1 r:3 我:1 ,:1 [ect] 到目前为止,我已经能够读取文件并计算单词,行数和字符数。 我还没有学过哈希图/表或树形图;寻找有关如何使用数组,数组列表或链表存储所有字符类型及其出现的建议

  • 我正在尝试使用javascript实现哈希表。目前,一切都正常,但我的get方法在给定特定键的哈希表中检索值时遇到了麻烦。我使用线性探测来避免冲突。当我散列键“亚历杭德罗”时,我将键映射到0索引。然后我将其添加到我的哈希表中。然后我尝试“Rosalby”,它也映射到0索引。我使用线性探测来找到下一个可用的插槽,在我的例子中,空索引是1,我将Rosalby的值放在那个插槽中。到目前为止,我似乎很好地

  • 本文向大家介绍python实现文本文件合并,包括了python实现文本文件合并的使用技巧和注意事项,需要的朋友参考一下 python合并文本文件示例代码。 python实现两个文本合并 employee文件中记录了工号和姓名 cat employee.txt: bonus文件中记录工号和工资 cat bonus.txt: 要求把两个文件合并并输出如下, 处理结果: 这个应该是要求用shell来写的

  • 问题内容: 我有一个文件,其内容为python列表的形式,如下所示: 有什么办法可以将python文件读回到列表对象中吗?而不是使用整个文件,而是将其读取为字符串。 编辑:对于那些可能有兴趣的人,我使用(import ast)遇到了一个奇怪的问题,作为解决上述问题的建议。 我在其中使用的程序具有从yahoo finance python模块获取历史股票数据的功能。此函数与ast.literal_e