简体   繁体   English

在Java中将对象附加到ArrayList中

[英]Appending an object in an ArrayList in java

I am working on an exercise and I ran into a problem. 我正在练习,却遇到了问题。

In NodeList , create a static method Node arrayToNode(String[] arr) which converts a String array into a list. NodeList ,创建一个static method Node arrayToNode(String[] arr) ,该方法将String数组转换为列表。 Your method should create a first Node , and then go through the rest of the array, creating a Node at each step, and using append to put the created Node at the end of the list. 您的方法应该创建第一个Node ,然后遍历数组的其余部分,在每个步骤中创建一个Node ,然后使用append将创建的Node放在列表的末尾。 Test this method on the command line arguments. 在命令行参数上测试此方法。 What happens if the array is empty? 如果array为空会怎样?

Currently my code is like this 目前我的代码是这样的

public static Node arrayToNode(String[] arr) {
    Node first = new Node(arr[0]);
    ArrayList<Node> list = new ArrayList<Node>();
    for(int i=1; i<arr.length;i++){
        list.add(new Node(arr[i]));
    }
}

as you can see there is no return statement YET. 如您所见,还没有return语句。 I am not sure if the person who wrote the exercise made a mistake by writing Node instead of void but I cannot ask him. 我不确定编写练习的人是否通过写Node而不是void来犯错,但是我不能问他。

The append method is append方法是

public void append(Node fin){
    if(next==null)
        next=fin;
    else
        append(next);
}

and the instance variables and the constructor is as follows: 实例变量和构造函数如下:

public String value;
public Node next;

public Node(String s){
    value =s;
    next=null;
}

I am quite unsure what it means to put the node at the end of the list as the ArrayList keeps on expanding. 我完全不确定随着ArrayList不断扩展,将节点放在列表的末尾意味着什么。 Also, I have questions about using the deploying the append method as in how to even use it in the TestNode class. 另外,我还有关于使用部署方法的问题,例如如何在TestNode类中使用它。


Thanks for the comment. 感谢您的评论。 I have now realized what the problem was and have made appropriate changes. 我现在意识到了问题所在,并进行了适当的更改。

public static Node arrayToNode(String[] arr){
    Node first = new Node(arr[0]);
    for(int i=1; i<arr.length;i++){
        Node nd = new Node(arr[i]);
        nd.append(nd);
        first.next=nd;
    }

    return first;
}

could you see if this is correct? 你能看一下这是否正确吗?

Unless I'm missing something, your append method should be this: 除非我缺少任何东西,否则您的append方法应为:

public void append(Node fin) {
    if (next == null)
        next = fin;
    else
        next.append(fin); // <- this line changed
}

That will append fin down the line until it reaches the end of the list while the way you have it in your OP will give infinite recursion. 这会将fin附加到该行的下方,直到到达列表的末尾,而在OP中使用它的方式将提供无限递归。

If it's supposed to be that way then creating the list is very simple. 如果应该这样,那么创建列表非常简单。 You can just append each value to the original one. 您可以将每个值附加到原始值。

public class Node {
    public static void main(String[] args) {
        Node begin = arrToLL(new String[] {
            "hello 1", "hello 2", "hello 3", "hello 4", "hello 5"
        });

        while (begin != null) {
            System.out.println(begin.val);
            begin = begin.next;
        }
    }

    static Node arrToLL(String[] arr) {
        if (arr == null) {
            return null;
        } else if (arr.length == 0) {
            return new Node("null");
        }

        int ind = 0;
        Node begin = new Node(arr[ind++]);

        while (ind < arr.length) {
            begin.append(new Node(arr[ind++]));
        }

        return begin;
    }

    /* instance */

    String val;
    Node next;

    Node(String val) { this.val = val; }

    void append(Node ap) {
        if (next == null) {
            next = ap;
        } else {
            next.append(ap);
        }
    }
}

Output is: 输出为:

hello 1
hello 2
hello 3
hello 4
hello 5

Inside the "to list" loop you could also "shuffle ahead" by assigning next to a variable like in my println loop. 在“ to list”循环中,您还可以像在我的println循环中一样,在变量next分配,以“随机播放”。 That way you aren't taking advantage of the "pass through". 这样,您就不会利用“通过”的优势。

public static Node arrayToNode(String[] arr){
    Node first = new Node(arr[0]);
    for(int i=1; i<arr.length;i++){
        Node nd = new Node(arr[i]);
        nd.append(nd); // <- appends nd to itself
        first.next=nd; // <- always assigns the new value to first
    }

    return first;
}

That's getting closer but I've commented the two lines that are in err. 距离越来越近,但我评论了错误的两行。 I think what you will end up with is: 我认为您最终将得到:

  • First Node with the first array element linked to 第一个节点与第一个数组元素链接到
  • A second Node with the last array element linked to 第二个节点,最后一个数组元素链接到
  • Itself (the second Node) 本身(第二个节点)

You can do this without append but you need another variable to shuffle: 您可以不append而执行此操作,但是需要另一个变量来随机播放:

public static Node arrayToNode(String[] arr){
    Node first = new Node(arr[0]);
    Node current = first;

    for(int i=1; i<arr.length;i++){
        Node nd = new Node(arr[i]);

        current.next = nd; // <- append the new node to the last
        current = nd; // <- shuffle ahead to the new one
    }

    return first;
}

Otherwise if I'm correct in thinking append had an error you can do something closer to my main example (including the shuffle if you want and a for loop works just as well). 否则,如果我认为append出现错误是正确的,那么您可以做一些更接近我的主要示例的操作(如果需要,可以包括随机播放,并且for循环也可以)。

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

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