【问题标题】:ArrayList is replacing item instead of adding it to endArrayList 正在替换项目而不是将其添加到末尾
【发布时间】:2021-06-27 20:42:11
【问题描述】:

我有一个方法可以在具有特定 id 的链表中找到一个节点并将项目添加到该节点的数组列表中。

ArrayList <String> elems;

public Place addElemToLst(String id, String elem) {
        
            // if no nodes create new node
            if (head == null) { 
                Node node = new Node(id);
                node.elems.add(item);
                head = unit;
            } else if (head != null) {
                Node curr = head;
                while (curr.next != null && !curr.next.id.equals(id)) {
                    curr = curr.next;
                }
                // if there is a id match
                if (curr.id.equals(id)) {
                    curr.elems.add(item);
                }
                // add new Node
                else { // the error is in this section
                    Node node = new Node(id);
                    node.elems.add(elem); 
                    curr.next = node;
                    
                }
        
        }

        return this;
    }

问题是当我在一个 ID 上多次调用 addElemToLst() 并说“item1”并继续向 arraylist 添加元素时,arraylist 只会保留最后一个输入到 arraylist 中的项目。本质上,arraylist 的大小始终为 1,因为以前的条目不断被替换。为什么会这样,错误是什么?我已将错误与代码中的注释隔离开来。 谢谢

【问题讨论】:

  • 可以添加缺少的代码吗?
  • item 在哪里定义?由于您没有在您的方法中创建它,您可能会一次又一次地添加相同的对象item。但这通常会导致您多次看到包含相同元素的列表。
  • 抱歉项目应该是 elem
  • 与问题无关,但“else if (head != null)”检查是多余的。一个简单的“else”会更好。

标签: java


【解决方案1】:

您没有检查第一个元素/头部是否有正确的 ID

 while (curr.next != null && !curr.next.id.equals(id))

下半场你可以试试这样的:

else {
        Node curr = head;
        Node prev = null;
        while(curr != null){                
            if(curr.id == id){
                curr.elems.add(elem);
                return this;
            }
            prev = curr;
            curr = curr.next;
        }
            Node node = new Node(id);
            node.elems.add(elem);
            prev.next = node;

        }
}

【讨论】:

    【解决方案2】:

    您查找具有匹配 ID 的节点的方法将返回一些不同的误报,这反过来会导致现有节点被覆盖。问题是您正在使用滑动窗口来检查节点并确定匹配项,但您的滑动标准与您确定是追加到现有节点还是创建新节点的标准不一致。

    这里有几个例子来说明:

      _ = null
      x = node
      o = node with matching ID
    [ ] = sliding window (left side is `curr`, right side is `curr.next`)
    
    x -> o -> _
    [    ]
    
    Since the matching node is at the end of the list, you should append
    `elem` to it. However, your code checks `curr.id` (instead of `curr.next.id`),
    incorrectly concludes that no matching node is found, and overwrites `curr.next`.
    
    o -> x -> _
    [    ]
         [    ]
    
    Here, the matching node is at the head of the list, but your code
    skips it and incorrectly creates a new node at the end of the list.
    
    o -> x -> x -> _
    [    ]
         [    ]
              [     ]
    Same as above, a new node is incorrectly inserted at the end of
    the list since you skipped the matching node at the head of the list.
    
    x -> o -> x -> _
    [    ]
    
    Since you're checking `curr.id` (instead of `curr.next.id`), your code
    incorrectly concludes that a new node needs to be created, overwriting
    the existing (matching) node and truncating the tail of the list.
    

    链接列表很难推理,结合while 条件使其更难理解。这是(IMO)更容易推理的固定实现:

    public Place addElemToLst(String id, String elem) {
        // if the list is empty, create a new node
        if (head == null) {
            Node node = new Node(id);
            node.elems.add(elem);
            head = node;
            return this;
        }
    
        Node curr = head;
        while (curr.next != null) {
            // if the matching node appears inside the list,
            // append the element and return
            if (curr.id.equals(id)) {
                curr.elems.add(elem);
                return this;
            }
    
            curr = curr.next;
        }
    
        // if the last node in the list is a match, use it
        if (curr.id.equals(id)) {
            curr.elems.add(elem);
        // now that you've exhausted all nodes, create a new one
        } else {
            Node node = new Node(id);
            node.elems.add(elem);
            curr.next = node;
        }
    
        return this;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2019-05-22
      相关资源
      最近更新 更多