您的位置:首页 > 编程语言 > Java开发

java 重定义数组的实现方法(与VB的ReDim相像)

2018-10-12 13:56 567 查看


//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;
}

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: