Java-操作反射其他的API
反射其它的API:
Class类中:
int getModifiers():获得修饰符
String getName():返回类的全限定名
Package getPackage():获得该类的包
String getSimpleName():获得类的简单名字
Class getSuperclass():获得类的父类
boolean isArray():判断该Class实例是否是数组
boolean isEnum() :判断该Class实例是否是枚举
Constructor,Method,Filed的信息:
去查阅相应类的API即可.
System类中,数组拷贝的方法:
public static native void arraycopy(Object src, int srcPos,
Obbject dest,int destPos,
int length);
/**
*参数
* src - 源数组
* srcPos - 源数组中的起始位置
* dest - 目标数组
* destPos - 目标数组中的起始位置
* length - 要复制的数组元素的数量
*/
public class ArrayDemo {
public static void main(String[] args) {
int[] src = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] dest = new int[10];
System.out.println(Arrays.toString(dest));
arraycopy(src, 4, dest, 3, 5);
System.out.println(Arrays.toString(dest));
}
public static void arraycopy(Object src, int srcPos, Object dest,
int destPos, int length) {
if (src == null || dest == null) {
throw new NullPointerException("源数组和目标数组不能为空");
}
if (!src.getClass().isArray() || !dest.getClass().isArray()) {
throw new ArrayStoreException("源和目标必须都是数组");
}
if (srcPos < 0 || destPos < 0 || srcPos + length > Array.getLength(src)
|| destPos + length > Array.getLength(dest)) {
throw new IndexOutOfBoundsException("索引越界");
}
if (src.getClass().getComponentType() != dest.getClass()
.getComponentType()) {
throw new ArrayStoreException("源数组和目标数组类型要一致");
}
for (int index = srcPos ;index<srcPos+length;index++ ) {
//获取需要拷贝的元素
Object val = Array.get(src, index);
//给目标数组设置元素
Array.set(dest, destPos, val);
destPos++;
}
}
}
共有 0 条评论