#Any ideas in why my array is printing like so?

31 messages · Page 1 of 1 (latest)

tired fern
#
        int [] nodeArray = new int[size]; // creates the new array
        recursiveToArray(head, nodeArray, 0); // calls the function to help make it recursive
        return nodeArray;
    }

    private void recursiveToArray(Node node, int[] nodeArray, int index) {
        if (node == null) {
            return;
        }
        // need to build the numbers of the list in the array
        nodeArray[index] = node.getData(); // adds value to the array
        recursiveToArray(node.getNext(), nodeArray, + 1); // get the next point and increase the index

    }```
daring graniteBOT
#

This post has been reserved for your question.

Hey @tired fern! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

rigid dome
#

That's the standard toString for an array

#

If you want to print the contents, you use Arrays.toString(array)

#

That's why intellij highlights that in yellow for you 🙂

tired fern
#

nodeArray.toString(nodeArray)? Huh hmm okay

rigid dome
#

no

#

that's not what I said

#

Arrays.toString is a static method

tired fern
#

?

#
        int [] nodeArray = new int[size]; // creates the new array
        recursiveToArray(head, nodeArray, 0); // calls the function to help make it recursive
        Arrays.toString(nodeArray);
        return nodeArray;
    }

    private void recursiveToArray(Node node, int[] nodeArray, int index) {
        if (node == null) {
            return;
        }
        // need to build the numbers of the list in the array
        nodeArray[index] = node.getData(); // adds value to the array
        recursiveToArray(node.getNext(), nodeArray, + 1); // get the next point and increase the index
        
    }```
#

but it still prints the same way

molten heron
#

Arrays.toString(array) returns a String that more helpfully describes the content of the array.

It doesn't perform any kind of magical modification on the array that would have for effect that the next time you call println() for it, it will print differently to how it would have otherwise.

#

So you would do:

System.out.println(Arrays.toString(nodeArray));
#

Technically, no one denies that it is an annoying decision that was made for array handling, and even more annoying fact of not overloading println() to take arrays and call Arrays.toString() for them by itself

tired fern
#

i get an error of cant resovle symbol

#

teacher doesnt want prints in the "main" code

molten heron
#

Yeah, Arrays utility class needs to be imported

#

Like adding import java.util.Arrays; at the beginning

#

As for prints somewhere, you're the one who made some and complained (justifiably) because it looks weird

#

I guess print it to see what it looks like then remove the print

tired fern
#

it is imported

#
        StringBuilder sb = new StringBuilder();
        recursiveToString(head, sb); // call the head and string to the function
        return sb.toString();
    }
    private void recursiveToString(Node node, StringBuilder sb) {
        if (node == null) { // if the list is empty add null
            sb.append("Null");
            return;
        }
        sb.append(node.getData() + " -> "); // add the data to the string
        recursiveToString(node.getNext(), sb);
    }``` thats my print land
molten heron
#

That's great and all, but are we still talking about your original issue?

tired fern
#

still not printing

#

i think its cause of this

#
        System.out.println("\nTesting testToArray");
        DoublyLinkedList doublyLinkedList = new DoublyLinkedList();
        doublyLinkedList.addFirst(6);
        doublyLinkedList.addLast(4);
        doublyLinkedList.addLast(3);
        doublyLinkedList.addLast(98);
        doublyLinkedList.addFirst(12);
        int[] array = doublyLinkedList;
        System.out.println(doublyLinkedList.toArray(doublyLinkedList));

    }```
molten heron
#

I don't see you calling Arrays.toString() anywhere

tired fern
#

.solved