次のようなことを行うジェネリックメソッドを実装したいと思います。
private <T> void addToSize(ArrayList<T> list, Class<T> type, int size) {
int currentSize = list.size();
for(int i = currentSize; i < size; i++) {
try {
list.add(type.newInstance());
} catch (InstantiationException e) {
logger.error("", e);
} catch (IllegalAccessException e) {
logger.error("", e);
}
}
}
上記の方法は、次のような場合に機能します。
ArrayList<Integer> test = new ArrayList<Integer>();
addToSize(test, Integer.class, 10);
しかし、私もそれが機能することを望んでいます...
ArrayList<ArrayList<Integer>> test = new ArrayList<ArrayList<Integer>>();
addToSize(test, ArrayList.class, 10); //Is this possible?
これは可能ですか?