简体   繁体   English

为什么会无限循环? (爪哇)

[英]Why would this loop infinitely? (Java)

I am trying to make an add method for a linked list, but for some reason (that is not obvious to me, in fact I came here to get help finding the error) it goes into an infinite loop every time.我正在尝试为链表创建一个 add 方法,但由于某种原因(这对我来说并不明显,实际上我来这里是为了帮助查找错误)它每次都会进入无限循环。

EDIT: I found the error, and I will keep my original code with a comment with the corrected code编辑:我发现了错误,我将保留我的原始代码,并带有更正代码的注释

public void insert(String majorName)
{
    MajorNode newNode = new MajorNode(majorName, 1);
    boolean inList = false;
    MajorNode current = first;

    if(isEmpty())
    {
        first = newNode;
                    // inList = true;
    }
    else
    {
        while(current.next != null)
        {
            if(current.majorName.equalsIgnoreCase(majorName))
            {
                current.frequency++;
                inList = true;
                break;
            }
            else
            {
                current = current.next;
            }
        }
    }

    if(!inList)
    {
        newNode.next = first;
        first = newNode;
    }
}

Here is my node class if it is needed:这是我的节点 class 如果需要的话:

public class MajorNode 
{
    public String majorName;
    public int frequency;
    public MajorNode next;

    public MajorNode(String majorName, int frequency)
    {
        this.majorName = majorName;
        this.frequency = frequency;
    }

    public String toString()
    {
        return majorName + " " + frequency;
    }
}

On the first call to insert() , one assumes isEmpty() returns true and consequently first is set to the newNode before newNode 's next field is set to the previous (null) value of first .在第一次调用insert()时,假设isEmpty()返回 true ,因此在newNodenext字段设置为first的前一个(空)值之前将first设置为newNode Thus, when the list is non-empty, the loop iterates indefinitely on the last element in the list whose next field points to itself.因此,当列表非空时,循环将无限地迭代列表中next字段指向自身的最后一个元素。

Out of curiosity, why are you trying to implement your own linked list functionality rather than build upon available packages (such as java.util.LinkedList<E> )?出于好奇,您为什么要尝试实现自己的链表功能,而不是基于可用的包(例如java.util.LinkedList<E> )?

When you create the first node you do this:创建第一个节点时,请执行以下操作:

if(!inList)
{        
    newNode.next = first;        
    first = newNode;    
}

This points the first nodes next at itself... hence a loop这将第一个节点指向下一个节点......因此是一个循环

You should be leaving the newNode.next as null for the first node, so that when you insert the second item, you reach the end of the chain..您应该将 newNode.next 保留为 null 作为第一个节点,这样当您插入第二个项目时,您就会到达链的末尾。

You will have an wrong frequency if you add a node which is similar to the last node of your List.如果您添加一个与列表的最后一个节点相似的节点,您将获得错误的频率。 Consider this situation (adding 2 similar nodes in the empty list)考虑这种情况(在空列表中添加2个相似节点)

  1. You will add a node1 in a blank list.您将在空白列表中添加一个 node1。 So first & current will point to node1.所以 first & current 将指向 node1。 (but node1.next will be null) (但 node1.next 将为空)
  2. If you add the same node (or a node with a same majorName ), you will reach to while loop (because List is not empty now).如果添加相同的节点(或具有相同majorName的节点),您将到达 while 循环(因为 List 现在不为空)。 And also, you will not enter into a while loop as well.而且,您也不会进入 while 循环。 (as your current.next is still null) and you will end up with two noes with same majorName in your list. (因为您的 current.next 仍然为空)并且您最终会在列表中得到两个具有相同majorName的 noes。

I would suggest to use我建议使用

while(current != null)

instead of代替

while(current.next != null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM