This is the array into which the elements of the set should be stored, if it is large enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Object [] toArray () returns an Object[] that cannot be passed to String[] or any other type array.
T [] toArray (T [] a) returns an array containing all the elements of this set; execution type of the returned array - the type of the specified array. If the set matches the specified array, it is returned in it. Otherwise, the type of the execution environment of the specified array and the size of this set are assigned to the new array.
If you execute the implementation code (I am posting the code from OpenJDK ), it will be clear to you:
public <T> T[] toArray(T[] a) { if (a.length < size)
source share