失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > java 实现重定义数组类似于VB的ReDim

java 实现重定义数组类似于VB的ReDim

时间:2024-05-03 06:04:13

相关推荐

java 实现重定义数组类似于VB的ReDim

码农一个,直接上代码: //paramobjArr 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; }

如果觉得《java 实现重定义数组类似于VB的ReDim》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。