Java 从LinkedList中删除项目

Java 从LinkedList中删除项目,java,linked-list,hashmap,Java,Linked List,Hashmap,我这里的程序有一个小问题。我正在尝试创建一个程序,根据hashCode(由hashFunction确定)将单词添加到数组中的链表中。如果它们的哈希代码具有相同的值,则会将它们添加到链接列表中。我有一个小计数方法,可以计算一个单词在列表中的次数。它通过计算hashFunction的值来工作。然后,它转到数组中的该值,并在LinkedList中迭代,直到达到空值。它有一个count变量,每次在列表中找到单词时,该变量都会递增。这是我的代码: public class test{ publi

我这里的程序有一个小问题。我正在尝试创建一个程序,根据hashCode(由hashFunction确定)将单词添加到数组中的链表中。如果它们的哈希代码具有相同的值,则会将它们添加到链接列表中。我有一个小计数方法,可以计算一个单词在列表中的次数。它通过计算hashFunction的值来工作。然后,它转到数组中的该值,并在LinkedList中迭代,直到达到空值。它有一个count变量,每次在列表中找到单词时,该变量都会递增。这是我的代码:

public class test{

    public static class Node<T>{
       public T data;
       public Node<T> next;
       public Node(){

       }

       public Node(T data, Node<T> next)
       {
          this.data = data;
          this.next = next;
       }
    }

    static Node[] array = new Node[512];

    public static void add(String word){
        int position = hashFunction(word);
        if(array[position] == null){
            array[position] = new Node(word, null);
        }else{
            Node newHead = new Node(word, array[position]);
            array[position] = newHead;
        }
    }

    public static void remove(String word){
        int remove = hashFunction(word);
        Node head = array[remove];
        if(head.data == word){
            head = head.next;
            System.out.println("Found");
        }else if(head.data != word){
            for(; array[remove] != null; array[remove] = array[remove].next){
                if(array[remove].data == word){
                    array[remove] = array[remove].next;
                }
            }
            System.out.println("Yusuf");
        }
    }

    public static int count(String word){
        int number = 0;
        int position = hashFunction(word);
        for(; array[position] != null; array[position] = array[position].next){
            if(array[position].data == word){
                number++;
            }
        }
        System.out.println(number);
        return number;
    }

    public static int hashFunction(String a){
        int sum = 1;
            for(int i = 0; i<a.length(); i++){
                char b = a.charAt(i);
                int value = (int) b;
                sum *= value;
         }
         return sum % array.length;
     }

    public static void addthings(String word, int n){
        for(int i = 0; i<n; i++){
            add(word);
        }
    }

    public static void main(String[] args) {
        addthings("abc", 500000);
        count("abc");
        count("abc");
        count("abc");
        count("abc");
        }
}
公共类测试{
公共静态类节点{
公共数据;
公共节点下一步;
公共节点(){
}
公共节点(T数据,下一个节点)
{
这个数据=数据;
this.next=next;
}
}
静态节点[]数组=新节点[512];
公共静态无效添加(字符串字){
int位置=哈希函数(字);
if(数组[位置]==null){
数组[位置]=新节点(字,空);
}否则{
Node newHead=新节点(字、数组[位置]);
数组[位置]=新头;
}
}
公共静态无效删除(字符串字){
int remove=hashFunction(word);
节点头=数组[移除];
if(head.data==word){
head=head.next;
System.out.println(“找到”);
}else if(head.data!=word){
for(;数组[remove]!=null;数组[remove]=数组[remove]。下一步){
if(数组[remove].data==word){
数组[remove]=数组[remove]。下一步;
}
}
System.out.println(“优素福”);
}
}
公共静态整数计数(字符串字){
整数=0;
int位置=哈希函数(字);
对于(;数组[position]!=null;数组[position]=数组[position]。下一步){
if(数组[position].data==word){
数字++;
}
}
系统输出打印项次(编号);
返回号码;
}
公共静态int哈希函数(字符串a){
整数和=1;

对于函数中的(int i=0;i,如果您编写了一些
Node head
,则表示您正在为节点创建一些本地实例。如果您设置
head=head。下一步
,这将仅更改本地实例变量的状态,而不是数组的状态

如果要检查第一个节点是否包含正在查找的数据并尝试将其删除,则必须将其从源数组(引用所在的数组)中删除。因此,可以编写如下内容:

if(head.data == word)
   array[remove] = head.next;
这是一个例子,关键是你不是在改变数组中的东西,而是在改变局部变量中的东西

 public static void remove(String word){
            int remove = hashFunction(word);
            Node head = array[remove];
            if(head.data == word){
                head = head.next;
                System.out.println("Found");
            }else if(head.data != word){
                for(; array[remove] != null; array[remove] = array[remove].next){
                    if(array[remove].data == word){
                        array[remove] = array[remove].next;
                    }
                }
                System.out.println("Yusuf");
            }
        }
第二个错误是在第二个子句中,您刚刚设置了
array[remove]=array[remove];
它会将你的linkedlist分为两个不同的linkedlist。假设你在linkedlist中有4个元素
A、B、C、D
,然后你删除
B
,并且有这样的指针
A->B->C->D
,那么你就不会从
A->C
中添加任何指针了。在这里,你可以断开你的linkedlist


您可以使用容易工作的while循环。

第一次调用时,会执行count()方法中的循环,因此会返回500000,但在随后的每次调用中,它都不会进入循环,因此它意味着数组[position]!=null;的计算结果为false,我将继续查看发生了什么,晚餐后将发回。在count()中的循环首次初始化后,此行:array[position]=array[position]。接下来将null分配给数组[38],并且在第一次调用loop/count()时它不是null,第一次数组[38](array[position])is:com.company.Test$Node@4c75cab9对于之后的每个调用,它都是null,所以array[position]!=null;是false,并且您的循环不会被执行,所以number保持在它的初始值0。