/*
* Same with the first one, but for 2D arrays.
*
*/
public K[][] increment(K[][] k){
int i = 0;
int ii = 0;
K[][] tmp = (K[][])new Object[Array.getLength(k)+1][];
for(K[] kara: k){
// instantiate a new array at i with the length of the current iteration ( kara )
tmp[i] = (K[])new Object[Array.getLength(kara)];
for(K kay: kara){
// fill tmp's array at i with the same values of kara
tmp[i][ii]=kay;
++ii;
}
++i;
ii=0;
}
return tmp;
}
In my program I often find myself adding entires to arrays but of different types. I want to add this to my utility class, so I can just instantiate the type using a generic and increment my arrays with ease. I do not need to refrence each array in the parameter 2D array just copy so I can return an array exactly the same as the one in the paremeter, but the length should be greater by one.
I instantiate after the method.