0

Consider the following simple code that converts a typed List to a typed array:

public static <T> T[] toArray(List<T> list) {
    T[] array = (T[]) new Object[list.size()];
    int index = 0;
    for(T t: list) {
        array[index++] = t;
    }

    return array;
}

Since it's not possible to instantiate a generic type primitive array as such:

T[] array = new T[list.size()];

it's necessary to cast the generic type array T[] to an object array Object[]. However, this produces an "unchecked cast" warning in an IDE. What is the optimal solution to this?

Joobsies
  • 21
  • 4

0 Answers0