ArrayCopy方法int数组、String数组使用实例

1

package com.szsm.swing.other;

import java.lang.reflect.Array;

public class Answer_1606 {

	public static void main(String[] args) {

		Test test = new Test();

		test.is = (int[]) addArrayLength(test.is, 10);
		test.ss = (String[]) addArrayLength(test.ss, 10);
		test.print();

	}

	public static Object addArrayLength(Object array, int newLength) {
		Object newArray = null;
		Class componentType = array.getClass().getComponentType();
		newArray = Array.newInstance(componentType, newLength);
		System.arraycopy(array, 0, newArray, 0, Array.getLength(array));
		return newArray;
	}
}

class Test {

	public int[] is = { 1, 2, 3 };

	public String[] ss = { "A", "B", "C" };

	public void print() {

		for (int index = 0; index < is.length; index++) {
			System.out.println("is[" + index + "]=" + is[index]);
		}

		System.out.println();

		for (int index = 0; index < ss.length; index++) {
			System.out.println("ss[" + index + "]=" + ss[index]);
		}

		System.out.println();

	}

}
2

is[0]=1
is[1]=2
is[2]=3
is[3]=0
is[4]=0
is[5]=0
is[6]=0
is[7]=0
is[8]=0
is[9]=0

ss[0]=A
ss[1]=B
ss[2]=C
ss[3]=null
ss[4]=null
ss[5]=null
ss[6]=null
ss[7]=null
ss[8]=null
ss[9]=null

是否调用2
是否调用
is[0]=1
is[1]=2
is[2]=3
is[3]=0
is[4]=0
is[5]=0
is[6]=0
is[7]=0
is[8]=0
is[9]=0

ss[0]=A
ss[1]=B
ss[2]=C
ss[3]=null
ss[4]=null
ss[5]=null
ss[6]=null
ss[7]=null
ss[8]=null
ss[9]=null



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静山晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值