Java 数组复制

 1 public static void main(String[] args) {
 2         int[]array1,array2;
 3         array1 = new int[] {6,3,5,8,9,5,17,5,58,56};
 4         
 5         for(int i =0; i<array1.length;i++)
 6         {
 7             System.out.print(array1[i]+" ");
 8         }
 9         System.out.println();
10 
11         //赋值array2变量等于array1
12         //不能称作 数组 复制
13         array2 = array1;    //地址赋值
14         //修改array2中的 偶索引元素,使其等于索引值(如array[0] = 0,array[2] = 2)
15         
16         for(int i =0; i<array2.length;i++)
17         {
18             if(i % 2==0)
19             {
20                 array2[i] = i;
21             }
22         }
23         //System.out.println();
24         
25 
26         for(int i =0; i<array1.length;i++)
27         {
28             System.out.print(array1[i]+" ");
29         }
30         System.out.println();
31         
32         System.out.println();
33         
34         
35         
36         //数组的复制:
37         int[]array3,array4;
38         array3 = new int[] {6,3,5,8,9,5,17,5,58,56};
39         
40         for(int i =0; i<array3.length;i++)
41         {
42             System.out.print(array3[i]+" ");
43         }
44         //System.out.println();
45         
46         
47          //数组的复制
48         array4 = new int[array3.length];
49         for(int i =0;i <array4.length;i++)
50         {
51             array4[i] = array3[i];
52         }
53         
54         
55         
56         
57         //修改array4中的 偶索引元素,使其等于索引值(如array[0] = 0,array[2] = 2)
58         for(int i =0; i<array4.length;i++)
59         {
60             if(i % 2==0)
61             {
62                 array4[i] = i;
63             }
64         }
65         System.out.println();
66         
67 
68         for(int i =0; i<array3.length;i++)
69         {
70             System.out.print(array3[i]+" ");
71         }
72         System.out.println();
73             
74         
75         
76         
77         System.out.println();
78         String[] arr = new String[] {"AA","BB","CC","DD","EE","FF"};
79         for(int i =0; i <arr.length;i++)
80         {
81             System.out.print(arr[i]+"  ");
82         }
83         System.out.println();
84         
85         //复制的操作
86          String[] arr2 = new String[arr.length] ;
87          for(int i =0; i<arr.length;i++)
88          {
89              arr2[i] = arr[i];
90          }
91         
92         for(int i =0; i <arr2.length;i++)
93         {
94             System.out.print(arr2[i]+"  ");
95         }
96         System.out.println();

 

posted on 2021-09-03 18:57  Bytezero!  阅读(192)  评论(0编辑  收藏  举报