当前位置: 移动技术网 > IT编程>开发语言>Java > java 重定义数组的实现方法(与VB的ReDim相像)

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

复制代码 代码如下:

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网