Java--(三)反射之编写泛型数组代码

日常学习仍旧在反射这部分,分享一下今天的学习心得:使用反射编写泛型数组代码

Java.lang.reflect包中的Array类允许动态的创建数组,其中copyOf方法可以用于扩展已经填满的数组

那么如何编写一个通用的方法呢,开始进行编写

TestOne:

private static Object[] badCopyOf(Object[] a, int newLength)//not useful
	{
		Object[] newArray = new Object[newLength];
		System.arraycopy(a, 0, newArray, 0, Math.min(a.length, newLength));
		return newArray;
	}

然后运行后出现一个问题,这段代码返回的数组类型是对象数组Obiect[ ]类型(例如Employee[ ]数组就不能转换 ),这样会产生一个异常,所以得到一个结论:一个从开始就是Obiect[ ]类型的数组永远不会转换成Employee[ ]数组。

想要通过转换该怎么办,这时就需要能够创建与原数组类型相同的新数组,需要Array类中的静态方法newInstance,它能构建新数组,调用时必须提供两个参数,一个是数组的元素类型,一个是数组的长度

接下来又是痛苦打代码时刻,我把不懂的方法使用和翻译写到了注释里面:

/**
 * This program demonstrates(演示了) the use of reflection for manipulating(操纵) arrays
 * @author 李阳
 *
 */
package com.reflection;

import java.lang.reflect.Array;
import java.util.Arrays;

public class CopyOfTest {
	public static void main(String[] args) {
		int[] a = {1,2,3};
		a = (int[]) goodCopyOf(a,10);
		System.out.println(Arrays.toString(a));
		
		String[] b = {"Tom","Dick","Harry"};
		b = (String[]) goodCopyOf(b,10);
		System.out.println(Arrays.toString(b));
		//下面调用将产生一个异常
		System.out.println("The following call will generate an exception.");
		b = (String[]) badCopyOf(b,10);
	}
	
	/**
	 * *This method grows an array by allocating a new array of the same type and 
	 * copying all element
	 * @param a
	 * @param newLength
	 * @return larger array that contains all elements of a .However ,the returned array
	 * hay type Object[], not the same type as a.返回的数组类型为 Object[],与a类型不相同。
	 */
private static Object[] badCopyOf(Object[] a, int newLength)//not useful
	{
		Object[] newArray = new Object[newLength];
		System.arraycopy(a, 0, newArray, 0, Math.min(a.length, newLength));
		return newArray;
	}


/**
 * This method grows an array by allocating a new array	 of the same type and 
 * copying all element  该方法通过分配相同类型的新数组并复制所有元素来生成数组
 * @param a
 * @param newLength
 * @return a larger array that contains all elements of a
 */
	private static Object goodCopyOf(Object a, int newLength) {
		Class cl = a.getClass();
		if (!cl.isArray()) return null;
		//使用Class类(只能定义表示数组的类对象)getComponentType的方法确定数组对应的类型
		Class componentType = cl.getComponentType();
		int length = Array.getLength(a);
		//Array类中的静态方法newInstance它能构造新数组,调用时必须提供两个参数,一个是数组的元素类型,一个是数组的长度;
		Object newArray = Array.newInstance(componentType, newLength);
		
		 /* array.copy(Object src,int srcPos,Object dest, int destPos,int length);
		 Copies an array from the specified source array, beginning at the 
		 specified position, to the specified position of the destination array.
		   从指定的源数组复制一个数组,从该数组开始指定位置,到目标数组的指定位置。*/
		System.arraycopy(a, 0, newArray, 0, Math.min(length, newLength));
		
		return newArray;
	}
}

打印一下结果:

[1, 2, 3, 0, 0, 0, 0, 0, 0, 0]
[Tom, Dick, Harry, null, null, null, null, null, null, null]
The following call will generate an exception.

最后提示一下,copyOf方法可以用来扩展任意类型的数组,而不仅仅是对象数组

int[] a = {1,2,3,4,5};
a = (int[]) goodCopyOf(a,10);

为了实现上面操作,应该将goodCopyOf的参数声明为Object类型,而不要声明为对象型数组Obiect[ ]。整型数组类型int[ ]可以被转换成Object类型,但不能转换成对象数组。

希望分享给像我一样在Java学习道路上的莘莘学子 加油!


小伙伴们可以关注我的公众号,留言必回复哦

Java核心基础

----------------------------------

长按关注哦(看那两撇小胡子)

基础 | 心得 | 经历 | 更重要的是开心嘛!


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值