java - 填充并打印个人 LinkedList 复制品的元素

标签 java list abstract-class

这会有点难以说明,因为代码是分开在几个类之间的,但我自己无法弄清楚。

此代码应该首先打印“此列表为空”。然后在一行中打印 stringData 中的每个单词:

public class Main
{
    public static void main(String[] args)
    {
        LinkedList_1 list1 = new LinkedList_1(null);
        list1.printList(list1.getRoot());

        String stringData = "Harrier Ken Klaasje Titus Evrart Manaaba Cindy Eyckhead";
        String[] data = stringData.split(" ");
        for (int i = 0; i < (data.length); i++) {
            list1.addItem(new Node(data[i]));
        }
        list1.printList(list1.getRoot());
    }
}

代码打印“此列表为空。”一次然后似乎永远不会到达第二个打印输出行,但没有异常(exception),通知,任何东西。我怀疑问题隐藏在list1.addItem中,它看起来像这样(这些评论仅供我自己使用):

        @Override
    public boolean addItem(ListItem newItem)
    {
        if (this.root == null) { // If no other item on list, newItem = first item on list
            this.root = newItem;
            return true;
        }
        ListItem current = this.root; // current = first item on list
        while (current != null) { // As long as current exists, do
            int comparison = (current.CompareTo(newItem)); // Alphabetically compares newItem and current. >0 = newItem comes AFTER current. <0 = newItem comes BEFORE current. 0 = newItem and current are identical.
            // ........................................................................................................
            if (comparison < 0) { // newItem should be inserted AFTER current
                if (current.getNext() != null) { // If the list contains another item after current, then
                    current = current.getNext(); // current becomes next item in list.
                    // outcome AFTER-NOT LAST YET is completed and the while loop will repeat until newItem is either last or no longer comes after
                } else { // if current is only item on list, then
                    current.setNextItem(newItem); // newItem comes after current
                    newItem.setPreviousItem(current); // current comes before after
                    return true; // outcome AFTER-LAST completed ---> drop out of the method/loop
                } // ........................................................................................................
            } else if (comparison > 0) { // newItem comes BEFORE current
                if (current.getPrevious() != null) { // if current is not first in list, then
                    current.getPrevious().setNextItem(newItem); // newItem comes after item that comes before new item (1. previous 2. newItem)
                    newItem.setPreviousItem(current.getPrevious()); // previous comes before newItem
                    newItem.setNextItem(current); // current comes after newItem (1.previous 2. newItem 3. current)
                    current.setPreviousItem(newItem); // newItem comes before current
                    // outcome BEFORE-NOT FIRST YET is completed and the while loop will repeat until newItem is either first or no longer comes before
                } else { // if current is first in list
                    newItem.setNextItem(this.root); // newItem comes before root (=current)
                    this.root.setPreviousItem(newItem); // root (=current) comes after newItem
                    this.root = newItem; // newItem is now root
                    return true;  // outcome BEFORE-FIRST completed ---> drop out of the method/loop
                }// ........................................................................................................
            } else { // curent == newItem. As a duplicate, newItem is not added to the list
                System.out.println(newItem.getValue() + " is already part of the List.class Duplicate not added.");
                return false; // drop out of the method/loop without adding newItem to the list
        }   }
        return false; // drop out of the loop without adding newItem to the list (how you would ever get here I don't know
    }

现在也许有人已经发现了问题,或者可能它位于其他类之一。由于我不想将整个代码(尽管这是一个相当短的训练程序)粘贴到新线程中,也许您可​​以告诉我缺少哪些信息来理解问题,然后我添加或删除特定的部分?

无论如何,先感谢您的帮助。

再想一想,让我立即添加 .printList 的功能。

    public void printList(ListItem root)
    {
        if (root == null) {
            System.out.println("This list is empty.");
        } else {
            while (root != null) {
                System.out.println(root.getValue());
                root = root.getNext();
            }
        }
    }

ListItem 类:

public abstract class ListItem
{
    protected ListItem previousItem = null;
    protected ListItem nextItem = null;
    protected Object value;

    public ListItem(Object value) {
        this.value = value;
    }
    public Object getValue() {
        return value;
    }
    public void setValue(Object value) {
        this.value = value;
    }

    abstract ListItem getNext();
    abstract ListItem getPrevious();
    abstract void setNextItem(ListItem n);
    abstract void setPreviousItem(ListItem p);
    abstract int CompareTo(ListItem item);
}

节点类:

public class Node extends ListItem
{

    public Node(Object value) {
        super(value);
    }

    @Override
    ListItem getNext() {
        return this.nextItem;
    }
    @Override
    ListItem getPrevious() {
        return this.previousItem;
    }
    @Override
    void setNextItem(ListItem n) {
        this.nextItem = n;
    }
    @Override
    void setPreviousItem(ListItem p) {
        this.previousItem = p;
    }
    @Override
    public Object getValue() {
        return value;
    }

    @Override
    int CompareTo(ListItem item)
    {
        if (item != null) {
            return ((String)super.getValue()).compareTo((String)item.getValue());
        } else {
            return -1;
        }
    }
}

最佳答案

在一种情况下您忘记返回,这就是它进入无限循环的原因。

    if (comparison > 0) { // newItem comes BEFORE current
                if (current.getPrevious() != null) { 
                    current.getPrevious().setNextItem(newItem); 
                    newItem.setPreviousItem(current.getPrevious()); 
                    newItem.setNextItem(current); 
                    current.setPreviousItem(newItem); 
                    // return when element is added successfully

关于java - 填充并打印个人 LinkedList 复制品的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59721459/

相关文章:

java - 更改字符串中重复的字母

python - 从python中的列表列表中选择一个子列表

c# - 了解列表中的继承项和基本项

java - 这种情况下如何处理ConcurrentModificationException

c++ - 如何创建抽象类的动态数组?

ruby-on-rails - 跳过继承模型中的回调

java - 如何使用Java URL和URL连接类打开网站

java - arraylist 与数组中原始类型的包装器

java - 添加 GitLab 私有(private)仓库作为 Maven 依赖

c++ - 如何创建一个模板参数是抽象基类的队列?