【问题标题】:Trying to convert an ArrayList to a LinkedList. Not sure why this does not work试图将 ArrayList 转换为 LinkedList。不知道为什么这不起作用
【发布时间】:2013-01-03 12:17:35
【问题描述】:

编辑:不知道为什么,但代码现在似乎在没有任何编辑的情况下工作。 jGrasp 调试器可能有问题吗?

===

好的。所以这是我的家庭作业,将在 2 周后分配,但我想要一个良好的开端。请不要更正我的代码,或分享正确的代码。如果你能指出我所做的错误,那就太好了。

所以我有一个带有以下构造函数的node

public node(String name)
public node(String name, node next)

我需要在一个单独的类中编写一个方法public method(ArrayList<String> names),它将names中的所有元素添加到链接列表中。

这是我现在拥有的:

public method(ArrayList<String> names) {
    if(names.size() == 0 || names == null) {
        throw new IllegalArgumentException();
    }

    // Handle base case, create first node
    first = new node(names.get(0));    // first has been declared above

    node current = first;

    // Add at the end of the list
    for(int i = 1; i < names.size(); i++) {
        current.next = new node(names.get(i));
        current = current.next;
    }

}

我不确定为什么这不能按要求工作。我正在使用 jGrasp,并使用调试器,我看到最后,我得到了一个只有 1 个值的链表(ArrayList 中的最后一个元素)。为什么?

请不要推荐使用任何高级功能,因为我是 Java 新手,使用任何进一步的高级功能只会让我感到困惑。

【问题讨论】:

  • 你的method不需要返回类型吗?
  • 变量首先应该保存整个链表。确保您不只是返回当前,这是链表的末尾。
  • 您如何测试您的链接列表是否包含单个值,使用属性(在上面的代码中未更新)或通过迭代从firstcurrent.next == null 的所有项目?如果您使用的是最后一种方法,请显示您在此方法中使用的代码。请注意您发布的代码,它至少应该可以编译。
  • 另外,不相关,但您的 names.size() == 0 || names == null 检查是倒退的。如果 names 为 null,则该检查的第一部分将抛出 NPE。
  • 请显示更多代码?我们看不到您将当前节点添加到链表的位置。 +1 表示您不想要现成的解决方案

标签: java arraylist linked-list


【解决方案1】:

我使用您的代码(并使用JavaBean standard naming)进行了测试,您的方法运行良好。这是代码示例(这里有一些长代码块):

import java.util.ArrayList;

class Node {
    private String data;
    private Node next;

    public Node(String data) {
        this.data = data;
        this.next = null;
    }

    public Node(String data, Node next) {
        this.data = data;
        this.next = next;
    }

    public String getData() {
        return data;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

public class NodeTest {

    private Node first;

    public NodeTest() {
        this.first = null;
    }

    //hint: this is your code, no changes were made here except by the method name
    public void insertArrayList(ArrayList<String> names) {
        //changing the order of the comparison. Java evaluates from left to right
        if(names == null || names.size() == 0) {
            throw new IllegalArgumentException();
        }

        // Handle base case, create first node
        first = new Node(names.get(0));    // first has been declared above

        Node current = first;

        // Add at the end of the list
        for(int i = 1; i < names.size(); i++) {
            current.setNext(new Node(names.get(i)));
            current = current.getNext();
        }
    }

    public void traverse() {
        Node current = first;
        while (current != null) {
            System.out.println(current.getData());
            current = current.getNext();
        }
    }

    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Hello");
        names.add("world!");
        NodeTest nodeTest = new NodeTest();
        nodeTest.insertArrayList(names);
        nodeTest.traverse();
    }
}

结果:

Hello
world!

因此,正如之前的 cmets 中所发布的那样,如果您的链接列表已被填充,或者您在未显示的代码中的其他地方存在问题,您如何测试可能存在问题。

【讨论】:

    【解决方案2】:

    我认为您正在从该方法返回最后一个节点,而您需要返回第一个节点,因为它包含所有进一步的链接节点。您应该返回第一个节点而不是当前节点。

    如果您仍然有问题,请向我们展示您是如何测试它以得出它仅包含最后一个元素的结论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-17
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2013-11-14
      • 2019-08-09
      • 1970-01-01
      相关资源
      最近更新 更多