本篇博文最后修改时间:2016年3月2日,22:17。
本篇介绍数组复制。
系统版本:Windows7 家庭普通版 32位操作系统。
三、版权声明
博主:思跡
声明:喝水不忘挖井人,转载请注明出处。
原文地址:http://blog.youkuaiyun.com/omoiato
联系方式:315878825@qq.com
Java零基础入门交流群:541462902
四、数组复制
范例1:数组复制操作
public class ArrayCopyDemo01
{
public static void main(String[] args)
{
int i1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; //源数组
int i2[] = {11, 22, 33, 44, 55, 66, 77, 88, 99}; //目标数组
copy(i1, 3, i2, 1, 3); //调用复制方法
print(i2); //输出数组
}
//参数含义:源数组名称、源数组开始点、目标数组名称、目标数组开始点、复制长度
public static void copy(int s[], int s1, int o[], int s2, int len)
{
for(int i = 0; i < len; i++)
{
o[s2 + i] = s[s1 + i]; //修改目标数组内容
}
}
public static void print(int temp[]) //数组输出
{
for(int i = 0; i < temp.length; i++)
{
System.out.print(temp[i] + "\t");
}
}
}
程序运行结果:
范例2:使用java类库中的方法完成数组复制操作
public class ArrayCopyDemo02
{
public static void main(String[] args)
{
int i1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; //源数组
int i2[] = {11, 22, 33, 44, 55, 66, 77, 88, 99}; //目标数组
System.arraycopy(i1, 3, i2, 1, 3); //Java对数组复制的支持
print(i2);
}
public static void print(int temp[]) //输出数组
{
for(int i = 0; i < temp.length; i++)
{
System.out.print(temp[i] + "\t");
}
}
}
程序运行结果: