There is a program which handles many lists of many types (Person, Cat, Chairs etc..).
The following code appears many times in the code. The code fills a given list with instances of type Cat
until the list has a given number of instances.
int howMany = max - cats.size();
for(int i = 0; i < howMany; ++i) {
cats.add(new Cat());
}
Since this code appears many time we want to create a method to replace it. The method will accept the List
that we want to add instances to and the max
number of instances. It can accept more parameters if needed.
This seems simple at first but then I remembered it's not possible to write new T()
because of type erasure.
What other way is there to achieve this?