Method to realize universal expansion of array length by reflection
import java.lang.reflect.Array;
public class Study {
public static void main(String[] args) {
text t = new text();
t.print();
text.i = (int[]) addArray(t.i, 10);
text.s = (String[]) addArray(t.s, 10);
t.print();
}
public static Object addArray(Object array, int langth) {
Object newArray = null;
Class obj = array.getClass().getComponentType();
newArray = Array.newInstance(obj, 10);
System.arraycopy(array, 0, newArray, 0, Array.getLength(array));
return newArray;
}
}
class text {
static int[] i = {
1, 2, 3 };
static String[] s = {
"c", "z", "m" };
public void print() {
for (int x = 0; x < i.length; x++) {
System.out.print("i[" + x + "]=" + i[x] + ",");
}
System.out.println();
for (int j = 0; j < s.length; j++) {
System.out.print("s[" + j + "]=" + s[j] + ",");
}
System.out.println();
}
}