Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+13 votes

I seem to have NameRecord.java working, but however when I go to print the array in the toString() method it prints out a memory location.

public String toString() {

	return "NameRecord[Name: " + name + ", Rankings: " + rankingData.toString() + "]";  
}

It prints out:
NameRecord[Name: Theresa, Rankings: [I@5ccd43cd]

I'm not quite sure what I'm doing wrong, I thought I initialized a new array in my constructor because all of my methods work, however I am unable to print out the array.

asked in CSC212_Spring2019 by (8 points)

1 Answer

+8 votes
 
Best answer

To display the contents of arrays in Java, you want to use:

Arrays.toString(arrayVar)

(where you import Arrays from java.util) instead of

arrayVar.toString()

because the built-in toString() method returns a string that just tells you what type of array it is (e.g. "capital i" for integer), and a rather arbitrary/unique id code (e.g. "5ccd43cd") that helps identify that array object (vs other array objects stored in memory).

Some more info here: https://www.geeksforgeeks.org/arrays-tostring-in-java-with-examples/

answered by (449 points)
selected by
+1

Thank you! Guess I had my head in a completely different place and was getting confused......this helped a lot!

...