我不是很有经验,我正在尝试处理哈希表中的冲突。然而

时间:2015-09-28 06:49:40

标签: java arrays hash collision string-hashing

我不是很有经验,我正在尝试处理哈希表中的冲突。但是,它只是跳过它而根本不写它。我认为带有条件的while循环如果会照顾它...我已经玩了一段时间了,感觉我混合了一切或​​者失去了理智。

    File file = new File("info.txt");
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String line;


        while ((line = br.readLine()) != null) {

            temp = nhash.hashing(line,maxSize);
            System.out.println(line + " " + " Hash key: " + temp);

            int loc = (int)temp;
            if(arr[loc] != null)       // I FIGURED THIS WOULD SOLVE it
            {                          // THE ISSUE... But IT DOESN'T
                while(arr[loc] != null) {
              //  System.out.println("Collision at [" + loc + "] with Data Item : [" + arr[loc] + "]");
                    loc++;
                    if(loc == maxSize)
                        loc = 0;
                }
            }else {
                arr[loc] = line;
             //   System.out.println("Data Item[" + line + "] Stored in index [" + loc + "] of array.");
                key[j] = loc;
                j++;
            }
        }

0 个答案:

没有答案