複製代碼代碼如下:
//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;
}