I use an init method per classes.
Spam[] spam1 = new Spam[13];
Spam[] spam2 = new Spam[7];
Spam[] spam3 = new Spam[5];
initSpamArray(spam1);
initSpamArray(spam2);
initSpamArray(spam3);
void initSpamArray (Object[] a) {
for (int i = 0, len = a.length; i < len; i++) {
a[i] = new Spam();
}
}
Ham[] ham1 = new Ham[13];
Ham[] ham2 = new ham[7];
Ham[] ham3 = new Ham[5];
initHamArray(ham1);
initHamArray(ham2);
initHamArray(ham3);
void initHamArray (Object[] a) {
for (int i = 0, len = a.length; i < len; i++) {
a[i] = new Ham();
}
}
Is it possible to define a kind of "universal" method to init any kind of object?
Like at least:
void initObjArray (Object[] a, <s.g. which suitable to transfer Class>) {
for (int i = 0, len = a.length; i < len; i++) {
a[i] = new <s.g. which suitable to transfer Class>();
}
}
I tried to Google a lot and to play with java reflection also (Object.getClass(); Constructor.newInstance(); Class.newInstance() ). However I have not been successful.