zoukankan      html  css  js  c++  java
  • java 实现重定义数组类似于VB的ReDim

    码农一个,直接上代码:  
    //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;
    }
  • 相关阅读:
    golang linux安装
    vscode 插件
    windows访问eks pods
    go mod包管理
    beego创建项目
    Atcoder ARC-125
    AtCoder AGC003 简要题解
    Python 字符串转成变量名
    13_Go基础(binary)
    12_Go基础(iota)
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/2993412.html
Copyright © 2011-2022 走看看