This question already has an answer here:
I'm new to programming. I'm sure the answer for this question is out there, but I have no idea what to search for.
Ok, I'll go right to it.
Here's my code:
int[] arr;
arr = new int[5];
arr[0] = 20;
arr[1] = 50;
arr[2] = 40;
arr[3] = 60;
arr[4] = 100;
System.out.println(arr);
This compiles and works fine. It's just the output from CMD that I'm dizzy about.
This is the output: [I@3e25a5
.
I want the output to represent the exact same numbers from the list (arr
) instead. How do I make that happen?
The current output is
classtype@hashcode
.Incase you need to print arrays with more than one dimension use:
Also remember to override
toString()
method for user-defined classes so that you get a representation of the objet as you choose and not the default represention which isclasstype@hashcode
My version of a shorter!
Use Arrays.toString() and PrintStream.printf(String format, Object... args).
It prints it's .toString() method you should print each element