I was doing this:
(String[]) myTreeSet.toArray();
but that gives me a ClassCastException at runtime.
The only thing I can think of doing is just making an array first and then iterating through each element in myTreeSet and adding it to the array. It seems like there must be a better way than this. Is there or should I just do this?
Thanks.
To convert a char to a string in Java, the toString() and valueOf() methods are used. The toString() and valueOf() methods are both used to convert any data type to a string. For converting a char to a string, they both function identically.
TreeSet. size() method is used to get the size of the Tree set or the number of elements present in the Tree set. Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Set.
TreeSet implements the SortedSet interface. So, duplicate values are not allowed. Objects in a TreeSet are stored in a sorted and ascending order. TreeSet does not preserve the insertion order of elements but elements are sorted by keys.
String[] result = myTreeSet.toArray(new String[myTreeSet.size()]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With