guava/src/com/google/common/collect/ObjectArrays.java
static <T> T[] toArrayImpl(Collection<?> c, T[] array) {
    int size = c.size();
    if (array.length < size) {
      array = newArray(array, size);
    }
    fillArray(c, array);
    if (array.length > size) {
      array[size] = null;
    }
    return array;
  }
static <T> T[] toArrayImpl(Collection<?> c, T[] array) {
    int size = c.size();
    if (array.length < size) {
      array = newArray(array, size);
    }
    fillArray(c, array);
    if (array.length > size) {
      array[size] = null;
    }
    return array;
  }
Returns an array containing all of the elements in the specified collection; the runtime type
of the returned array is that of the specified array. If the collection fits in the specified
array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the
specified array and the size of the specified collection.

<p>If the collection fits in the specified array with room to spare (i.e., the array has more
elements than the collection), the element in the array immediately following the end of the
collection is set to {@code null}. This is useful in determining the length of the collection
<i>only</i> if the caller knows that the collection does not contain any null elements.

<p>This method returns the elements in the order they are returned by the collection's
iterator.

<p>TODO(kevinb): support concurrently modified collections?

@param c the collection for which to return an array of elements
@param array the array in which to place the collection elements
@throws ArrayStoreException if the runtime type of the specified array is not a supertype of
the runtime type of every element in the specified collection