zoukankan      html  css  js  c++  java
  • 通过反射修改已有数组的大小

    import java.lang.reflect.Array;

    public class ChangeArrayDemo {
        public static void main(String[] args) {
            int temp[] = { 1, 2, 3 };
            int newTemp[] = (int[]) arrayInc(temp, 5);
            print(newTemp);
            System.out.println(" =============================");
            String t[] = { "von", "mldn", "java" };
            String nt[] = (String[]) arrayInc(t, 8);
            print(nt);
        }

        public static Object arrayInc(Object obj, int len) {
            Class<?> c = obj.getClass();
            Class<?> arr = c.getComponentType();
            Object newO = Array.newInstance(arr, len);
            int co = Array.getLength(obj);
            System.arraycopy(obj, 0, newO, 0, co);
            return newO;
        }

        public static void print(Object obj) {
            Class<?> c = obj.getClass();
            if (!c.isArray()) {
                return;
            }
            Class<?> arr = c.getComponentType();
            System.out.println(arr.getName() + "Array's length is:"
                    + Array.getLength(obj));
            for (int i = 0; i < Array.getLength(obj); i++) {
                System.out.print(Array.get(obj, i) + ",");
            }
        }
    }

  • 相关阅读:
    Linux 部署 nginx
    Linux 部署vue项目(使用nginx)
    git 操作规范
    mysql grant权限分配(转)。
    前端优化,包括css,jss,img,cookie
    关于js里的那一堆事件
    个人作业——软件工程实践总结作业
    Unity3D 快捷键
    Beta冲刺第二天
    Beta冲刺第一天
  • 原文地址:https://www.cnblogs.com/vonk/p/3956650.html
Copyright © 2011-2022 走看看