Java: Converting a set to an array for String representation
The code works fine.
Replace:
System.out.println(array);
With:
System.out.println(Arrays.toString(array));
Output:
[b, c, a][b, c, a]
The String
representation of an array displays the a "textual representation" of the array, obtained by Object.toString
-- which is the class name and the hash code of the array as a hexidecimal string.
for the sake of completeness check also java.util.Arrays.toString and java.util.Arrays.deepToString.
The latter is particularly useful when dealing with nested arrays (like Object[][]).
It's OK.
You are not seeing the array contents with System.out.println(array) because printlncalls object.toString() to get the bytes from an Object for output.
Since HashSet overrides the default toString() implementation, you can see the set contents with System.out.println(set);
As arrays do not override the default toString() (that gives the class name and some sort of identity hash code), you are getting the fuzzy [Ljava.lang.String;@9b49e6
Hope that helps