代碼如下:
//param objArr the expanded object of Array.
//param newLength the length of the new Array
public static Object getNewArr(Object objArr, int newLength) {
if (!objArr.getClass().isArray()) {//判斷類型
return null;
}
// get the array's componentType
Class componentType = objArr.getClass().getComponentType();//獲得類型
//get a newInstance of a Array object Object newArray = Array.newInstance(componentType, newLength);//新建數組對象
//copy the array
System.arraycopy(objArr, 0, newArray, 0, Array.getLength(objArr));//把原數組數據copy到新建數組中,其中newLength要大於元objArr的length,否則此句報錯
return newArray;
}