1 import java.util.Arrays;
2
3 public class Test01 {
4 public static void main(String[] args) {
5 // 数组拷贝方式1
6 // int[] a= {1,2,3,4,5};
7 // int[] b= new int[a.length];
8 // for(int i=0;i<a.length;i++) {
9 // b[i]=a[i];
10 // }
11 // for(int i=a.length-1,j=a.length/2;i>=a.length/2;i--,j++) {
12 // b[j]=a[i];
13 // }
14 // System.out.println(Arrays.toString(b));
15
16 //----------------------------
17 //数组拷贝方式2,Arrays.copyOf(要拷贝的原始数组,要拷贝的元素个数)
18 // int[] a=new int[] {1,2,-3,4,5};
19 // int[] b=Arrays.copyOf(a, a.length);
20 // System.out.println(Arrays.toString(b));
21 //
22 //----------------------------
23 //数组拷贝方式3,Arrays.copyOfRange(要拷贝的原始数组,起始下标from,截止下标to)
24 //拷贝的元素个数=to-from或者拷贝的元素的范围为:[from,to),能取到from,取不到to
25 // int[] a=new int[] {1,2,3,4,5};
26 // int[] b=Arrays.copyOfRange(a,0,1);
27 // System.out.println(Arrays.toString(b));
28
29 //----------------------------
30 //数组拷贝方式4,System.arraycopy(要拷贝的原始数组,原始数组的起始下标from,目标数组brr,目标数组的其实下标from2,要拷贝的元素的个数)
31 // int[] a= {1,2,3,4,5,6};
32 // int[] b=new int[a.length];
33 // System.arraycopy(a,1,b,3,3);
34 // //拷贝的数组元素个数<=数组b的length
35 // System.out.println(Arrays.toString(b));
36
37 //数组拷贝方式5,a.clone()
38 int[] a= {1,2,3,4,5};
39 int[] b=a.clone();
40 System.out.println(Arrays.toString(b));
41
42 }
43 }