最近正在看java核心技术,以前看过的,但基本上是大概浏览,并没有深入地去了解细节,只是学学语法,难的基本上是跳过去的。现在开始重新看了,希望系统地看一遍。看到反射时,书上用反射实现了数组动态增加长度,mark一下,供以后自己学习参考。实现如下:
/**
* 扩大数组长度10%+10
* @param a 数组
* @return 扩充的数组
*/
public static Object ArrayGrow(Object a)
{
Class class1=a.getClass();
if(!class1.isArray())
return null;
Class componentType=class1.getComponentType();
int length=Array.getLength(a);
int newLength=length*11/10+10;
Object newObj=Array.newInstance(componentType, newLength);
System.arraycopy(a, 0, newObj, 0, length);
return newObj;
}