【Java】 数组拷贝的通用方法
2015-02-12 15:21
369 查看
方法来源自《Java核心教程》,有一定改动。
Class<?> java.lang.Class.getComponentType(),获取元素类型。
Object java.lang.reflect.Array.newInstance(Class<?> componentType, int length) throws NegativeArraySizeException
函数原型,为反射数组对象创建新数组的静态方法。
void java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
System类提供的数组拷贝方法。
import java.lang.reflect.Array; import java.util.Arrays; public static Object MultiCopyOf(Object rhs, int newLength) { Class<?> tmp=(Class<?>) rhs.getClass(); if(!tmp.isArray()) { return null; } Object newArray=Array.newInstance(tmp.getComponentType(), newLength); System.arraycopy(rhs, 0, newArray, 0, Math.min(Array.getLength(rhs), newLength)); return newArray; }
Class<?> java.lang.Class.getComponentType(),获取元素类型。
Object java.lang.reflect.Array.newInstance(Class<?> componentType, int length) throws NegativeArraySizeException
函数原型,为反射数组对象创建新数组的静态方法。
void java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
System类提供的数组拷贝方法。
相关文章推荐
- java 对象深拷贝通用方法
- java数组拷贝native方法
- Java学习之数组1(1.数组的声明;2.元素为引用数据类型的数组;3.关于main方法里的String[] args;4.数组排序;5.数3退1 数组算法,(用数组模拟链表);6数组查找之二分法;7数组的拷贝)
- Java数组拷贝方法arraycopy()
- Java数组拷贝的四种方法
- java语言基础——System类的数组拷贝方法
- 【Core Java Volume 5】集合算法---查找数组、集合最大值的通用方法
- 【CORE JAVA】反射应用:通用方法-扩展数组长度
- Java基础知识强化85:System类之arraycopy()方法(数组拷贝)
- 【Java基础】System的arraycopy方法拷贝数组
- java数组的四种拷贝方法的性能分析:for、clone、System.arraycopy、Arrays.copyof
- java基础之数组的拷贝(自定义方法与System下的自带方法对比)
- JavaScript和Java数组拷贝各种方法耗时比较
- java数组拷贝的方法
- java 数组 、 for each循环, 数组的两种拷贝方法 ,Arrays.copyOf(),Arrays.toString[],匿名数组
- Java高级篇-6-数组拷贝方法
- java 对象深拷贝通用方法
- java数组的拷贝四种方法:for、clone、System.arraycopy、arrays.copyof
- java数组的拷贝四种方法:for、clone、System.arraycopy、arrays.copyof
- 关于Java 拷贝数组方法 Arrays.copyOf() 是地址传递还是值传递