In working on an Euler problem as a new Java programmer, I have encountered something peculiar related to printing an array.
I do not understand why this is happening - the individual values appear to be correct, when printed individually, but the toString()
is clearly creating a string which is not what I expect.
I would have expected either a compile error, or the array to be put into a concatenated list. Neither of these happened.
Note: I am not interested in "how to print an array?" but rather understanding why the toString()
does NOT print the array. There are plenty of resources online available to find how to do so.
public class example {
public static void main(String[] args) {
/* Setup a string/int array converion*/
int i=0;
String nums = "123456";
char[] splitNums = nums.toCharArray();
int[] ints = new int[nums.length()];
for (char c : splitNums) {
ints[i++] = Character.digit(c,10);
}
//Print using the string/char[]
System.out.println(nums);
System.out.println(splitNums);
//Values are clearly there
for (int j : ints){
System.out.println(j);
}
//What is toString() doing?
System.out.println(ints.toString());
}
}
Output:
123456
123456
1
2
3
4
5
6
[I@4b71bbc9