Trying to convert ArrayList to LinkedList. Don't know why it doesn't work

EDIT: Not sure why, but the code now works without any changes. Maybe a problem with the jGrasp debugger?

===

Ok. So this is my homework, which will be scheduled in 2 weeks, but I want to get started. Please do not modify my code or use the correct code. If you could point out a mistake in what I am doing that would be great.

So I have node

with the following constructors:

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

      

I need to write a method public method(ArrayList<String> names)

in a separate class that will add all the items from names

the linked list.

Here's what I have right now:

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;
    }

}

      

I'm not sure why this doesn't work as needed. I am using jGrasp and using the debugger I can see that at the end I get a linked list of only 1 value (the last item in the ArrayList). Why?

Please, it is not recommended to use any additional functions as I am new to Java and using any additional additional functions just confuses me.

+3


source to share


2 answers


I made a test using your code (and using standard JavaBean naming ) and your method works fine. Here's some sample code (some long block of code here):

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();
    }
}

      

Result:



Hello
world!

      

So, as pointed out in the previous comments, maybe there is a problem with the way you are testing if your linked list is full, or you have a problem somewhere else in the code not shown.

0


source


I think you are returning the last node from the method while you need to return the first one as it contains all the additional related nodes. You should return the first node not the current node.



If you have problems, please show us how you test it to conclude that it only contains the last item.

0


source







All Articles