数组copy方法

来源:互联网 发布:数据库流程 编辑:程序博客网 时间:2024/05/16 10:36
public class TestArrayCopy {
public static void main(String[] args) {
int[] a = {1, 2, 5, 7, 9, 10};
int[] b = new int[6];             //定义并初始化数组b,注意要new出长度
TestArrayCopy test = new TestArrayCopy();
System.out.print("a数组元素:");
test.print(a);
System.out.print("b数组元素:");
test.print(b);
System.arraycopy(a, 0, b, 0, a.length);           //copy  a从0位开始 到 b的0位开始,copy长度为a数组自身的长度 
System.out.print("b数组元素:");
test.print(b);
}


private void print(int[] num) {             //输出方法
for(int temp : num) {
System.out.print(temp + " ");
}
System.out.println();
}
}
0 0
原创粉丝点击