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
}```
#Any ideas in why my array is printing like so?
31 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @tired fern! Please use
/closeor theClose Postbutton 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.
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 🙂
nodeArray.toString(nodeArray)? Huh hmm okay
?
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
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
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
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
That's great and all, but are we still talking about your original issue?
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));
}```
I don't see you calling Arrays.toString() anywhere
.solved