I am trying to write a method that will take in an array and return another array of the same type, double the size and with the same contents. However I need a way of assigning the array type to something that will not be known until runtime. example:
public object[] doubleArray(object[] array) {
---[] arrayCopy = new ---[array.length*2];
//copy content
for (int i = 0; i < array.length; i++) {
arrayCopy[i] = array[i];
}
return arrayCopy;
}
The array passed in will not be known until runtime.
Thanks any help will be greatly appreciated
PS I realise that managing of array sizes can be done with ArrayLists etc but for this purposes it would be great to keep things to arrays.
What you can do for a generic array is
Note: this will work for all arrays including primitive arrays.