0

I am working on a problem and I was provided with the following method header:

T[] getAll(T[] elements)

I am not allowed to modify it.

The purpose of the method is to fill the elements array with elements from a member list. Before this is done however we need to check if the array's capacity is large enough for all the elements to fit. If it is not we have to create a new array with an appropriate capacity.

How do I create an array when I do not know which datatype the elements are? As far as I know it's not possible to create generic arrays.

@Override
public T[] getAll(T[] elements)
{
    // TODO create new array if needed

    for(int i = 0; i < size(); i++)
    {
        elements[i] = list.element();
        list.move();
    }

    return elements;
}
Jordan.J.D
  • 7,382
  • 9
  • 44
  • 75
Riketh
  • 37
  • 2
  • 8

0 Answers0