Can you explain why this code
int[] test={0,0,0,0,0};
System.out.println(test);
prints something like [I@42e816 (perhaps memory address) but this code
Stack<Integer> stack = new Stack<Integer>();
stack.push(1);
stack.push(3);
stack.push(5);
stack.push(2);
stack.push(4);
System.out.println(stack);
prints “[1, 3, 5, 2, 4]”? What’s the difference?
If Stacks derive from Vectors and Vectors from arrays, what’s the cause of this different behavior?
The collections have a well defined toString() method, but arrays were forgotten about IMHO and use the default Object.toString() along with lots of other default method from Object which are not very useful either. You need to call one of the many helper classes were added later to make arrays more useful.
The hexidecimal is the default hashCode() of the object rather than an address. It might not be unique and even if the array moves around the memory, this number won’t change.
For helper classes for arrays
for extra ones
and many more.
It not the case that arrays shouldn’t have their own methods but rather there is too many to choose from. 😉