【问题标题】:Eclipse suspends my process in debug modeEclipse 以调试模式暂停我的进程
【发布时间】:2020-03-08 13:24:57
【问题描述】:

所以我正在做一个涉及链接列表的项目。我们必须自己制作节点和链表(不允许使用 Java 提供的)。作为项目的一部分,我正在制作一个列表,该列表将根据某些标准自动调整(当输入的单词与已经存在的单词相同时,将该节点移动到列表的前面)。我的代码似乎运行良好,但经过一段时间后,它就停止运行。当我尝试调试它时,Eclipse 只是暂停了该过程,我不知道为什么,因为它完全没有提供任何反馈。它似乎在其中一个 while 循环中,但我似乎无法弄清楚原因。任何帮助将不胜感激。代码比较长,所以我把它贴在这堵文字墙下面。我在编程方面还不是很有经验,所以你可能会注意到一些错误/烦恼。

SelfAdjustingListOne.java

public class SelfAdjustingListOne extends UnsortedList
{
    public SelfAdjustingListOne()
    {
        super();
    }

    public SelfAdjustingListOne(long timer)
    {
        super(timer);
    }

    public void adjustingAdd(Node input)
    {
        // If there's nothing in the list, make this the first and last node
        if (getFront() == null)
        {
            setFront(input);
            setBack(input);
            input.setIndex(0);
        } else if (sameWord(input) != null)
        {
            // If the word already exists, increment the word count and send that node to
            // the front of the list
            Node sameString = sameWord(input), current = getFront(), previous;
            try
            {
                // Will return null if sameString is the first node on the list
                previous = getByIndex(sameString.getIndex() - 1);
            } catch (NullPointerException e)
            {
                previous = null;
            }
            // If sameString is the first node, no link needs to be set
            if (previous != null)
                previous.setLink(sameString.getLink());
            // Link the node we are moving to the front node
            sameString.setLink(getFront());
            // Set the value of the front node to the node we are moving
            setFront(sameString);
            // Increment its count
            sameString.plusCount();
            // While the current node exists and has not surpassed the previous location of
            // the node we moved, increment the index value of each node by 1
            while (current != null && current.getIndex() != sameString.getIndex())
            {
                current.plusIndex();
                current = current.getLink();
            }
            // Set the new front node's index to 0 (Beginning of the list)
            sameString.setIndex(0);
            plusComparisons();
            plusComparisons();
        } else
        {
            // If the list has at least one node and the word being added doesn't exist, add
            // this node to the front of the list
            input.setLink(getFront());
            Node current = getFront();
            while (current != null)
            {
                current.plusIndex();
                current = current.getLink();
            }
            setFront(input);
            input.setIndex(0);
            plusComparisons();
            plusNodeChanges();
            plusNodeChanges();
        }
    }
}

UnsortedList.java

import java.text.DecimalFormat;

public class UnsortedList
{
    private Node front;
    private Node back;
    private Long timer;
    private int numOfComparisons;
    private int nodeChanges;

    public UnsortedList()
    {

    }

    public UnsortedList(long timer)
    {
        this.timer = timer;
    }

    public void addBack(Node input)
    {
        if (front == null)
        {
            setFront(input);
            setBack(input);
            input.setIndex(0);
        } else if (sameWord(input) != null)
        {
            Node sameString = sameWord(input);
            sameString.plusCount();
            numOfComparisons += 2;
        } else
        {
            getBack().setLink(input);
            input.setIndex(back.getIndex() + 1);
            setBack(input);
            numOfComparisons++;
            nodeChanges += 2;
        }
    }

    public void addFront(Node input)
    {
        if (front == null)
        {
            setFront(input);
            setBack(input);
            input.setIndex(0);
        } else if (sameWord(input) != null)
        {
            Node sameString = sameWord(input);
            sameString.plusCount();
            numOfComparisons += 2;
        } else
        {
            input.setLink(front);
            Node current = front;
            while (current != null)
            {
                current.plusIndex();
                current = current.getLink();
            }
            setFront(input);
            input.setIndex(0);
            numOfComparisons++;
            nodeChanges += 2;
        }
    }

    public void remove(int index)
    {
        Node current = front;
        do
        {
            if (current.getIndex() == index - 1)
            {
                if (current.getLink().getLink() != null)
                {
                    current.getLink().setIndex(-1);
                    current.setLink(current.getLink().getLink());
                    Node currentIndexNode = current.getLink();
                    while (currentIndexNode != null)
                    {
                        currentIndexNode.minusIndex();
                        currentIndexNode = currentIndexNode.getLink();
                    }
                } else
                {
                    current.getLink().setIndex(-1);
                    current.setLink(null);
                }
            }
            current = current.getLink();
        } while (!current.isEqual(back));
    }

    public void setFront(Node input)
    {
        front = input;
    }

    public void setBack(Node input)
    {
        back = input;
    }

    public Node getFront()
    {
        return front;
    }

    public Node getBack()
    {
        return back;
    }

    public Node getByIndex(int index) throws NullPointerException
    {
        Node current = front, currentIndexNode = current.getLink();
        while (current != null)
        {
            do
            {
                if (current.getIndex() == index)
                    return current;
                current = currentIndexNode;
                currentIndexNode = currentIndexNode.getLink();
            } while (currentIndexNode != null);
        }
        return null;
    }

    public Node getByWord(String word) throws NullPointerException
    {
        Node current = front, currentIndexNode = current.getLink();
        while (current != null)
        {
            do
            {
                if (current.getWord().equalsIgnoreCase(word))
                    return current;
                current = currentIndexNode;
                currentIndexNode = currentIndexNode.getLink();
            } while (currentIndexNode != null);
        }
        return null;
    }

    public int totalWords()
    {
        Node current = front;
        int totalWords = 0;
        while (current != null)
        {
            totalWords += current.getCount();
            current = current.getLink();
        }
        return totalWords;
    }

    public int totalUniqueWords()
    {
        Node current = front;
        int totalUniqueWords = 0;
        while (current != null)
        {
            totalUniqueWords++;
            current = current.getLink();
        }
        return totalUniqueWords;
    }

    public int totalNumOfComparisons()
    {
        return numOfComparisons;
    }

    public int totalNodeChanges()
    {
        return nodeChanges;
    }

    public String totalTimeElapsed()
    {
        if (timer == null)
            return "This is an untimed list";
        DecimalFormat threePlaces = new DecimalFormat("#0.000");
        return threePlaces.format((System.nanoTime() - timer) * Math.pow(10, -9)) + " seconds";
    }

    public void plusComparisons()
    {
        numOfComparisons++;
    }

    public void plusNodeChanges()
    {
        nodeChanges++;
    }

    protected Node sameWord(Node input)
    {
        Node current = front;
        while (current != null)
        {
            if (current.getWord().equalsIgnoreCase(input.getWord()))
                return current;
            current = current.getLink();
        }
        return null;
    }
}

Node.java


public class Node
{
    private Node link;
    private String word;
    private int count = 1;
    private int index = -1;

    public Node(String word)
    {
        this.word = word;
    }

    public Node getLink()
    {
        return link;
    }

    public String getWord()
    {
        return word;
    }

    public int getCount()
    {
        return count;
    }

    public int getIndex()
    {
        return index;
    }

    public void setLink(Node input)
    {
        link = input;
    }

    public void setWord(String input)
    {
        word = input;
    }

    public void setCount(int input)
    {
        count = input;
    }

    public void setIndex(int input)
    {
        index = input;
    }

    public void plusCount()
    {
        count++;
    }

    public void plusIndex()
    {
        index++;
    }

    public void minusIndex()
    {
        index--;
    }

    public boolean isEqual(Node input)
    {
        if (input.getWord().equalsIgnoreCase(this.word))
            return true;
        return false;
    }
}

运行 SelfAdjustingListOne 的代码

public static SelfAdjustingListOne salo;
public static void main(String[] args)
    {
        System.out.println("Running fifth pass...");
        System.out.println("Time to execute fifth pass: " + pass5());
    }
public static String pass5()
    {
        salo = new SelfAdjustingListOne(System.nanoTime());
            try
            {
                Scanner scanner = new Scanner(new File(fileDirectory + fileNames[0] + fileExtension));
                while (scanner.hasNext())
                {
                    String s = scanner.next();
                    s.replaceAll("^[^a-zA-Z0-9]+", "");
                    s.replaceAll("[^a-zA-Z0-9]+$", "");
                    if (s.length() == 1 || s.length() == 0)
                    {
                        if (!Character.isAlphabetic(s.charAt(0)) && !Character.isDigit(s.charAt(0)))
                            continue;
                    }
                    salo.adjustingAdd(new Node(s));
                }
                scanner.close();
            } catch (FileNotFoundException e)
            {
                System.out.println("No file found matching that name/directory");
            }
        return salo.totalTimeElapsed();
    }

它说它正在读取的文件是 A Bee 电影脚本,由于帖子的最大长度,我无法发布,但任何文本文件都应该这样做。

【问题讨论】:

  • @samabcde 我很乐意提供一个例子,但我不太确定为什么会发生这种情况。我可以提供我的其余文件,以便重现问题。不幸的是,我无法创建一个会产生此问题的测试用例,因为我不知道它为什么会发生,这就是我在这里的原因。
  • 没有更多细节很难判断发生了什么,尝试在暂停代码之前添加断点并逐步运行以查看发生了什么you may reference this。由于您怀疑 while 循环正在暂停,请同时发布相关代码。
  • @samabcde 我会在业余时间尝试再次进行断点调试,但这就是我最初的问题所在。调试是 Eclipse 决定在没有警告的情况下挂起自己。如果您愿意,我发布了所有相关代码。感谢您迄今为止的关注和帮助。
  • Node 是来自库还是您制作的自定义类?我需要这个类来编译。

标签: java eclipse debugging linked-list


【解决方案1】:

我在 samabcde 的帮助下想通了。

这里的代码块需要改一下:

if (previous != null)
previous.setLink(sameString.getLink());
// Link the node we are moving to the front node
sameString.setLink(getFront());
// Set the value of the front node to the node we are moving
setFront(sameString);

对此:

if(sameString != getFront())
{
    sameString.setLink(getFront());
    // Set the value of the front node to the node we are moving
    setFront(sameString);
}

它正在链接到自己,因为它从未检查它设置链接的节点是否已经是列表中的第一个节点,因此将链接设置为等于自身。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 2017-04-23
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2011-02-17
    相关资源
    最近更新 更多