Java數組的復制全解


1、將一個基本數據類型數組的引用賦值給另一個數組

 1 public class Array_copy {
 2     int[] array1=new int[]{1,2,3,4,5,6};
 3     int[] array2=array1;//將array1的引用賦值給array2,兩數組指向同一個內存空間
 4     public static void main(String args[]){
 5         Array_copy ac = new Array_copy();
 6         for (int i=0;i<ac.array1.length;i++){
 7             System.out.print(ac.array1[i]+" ");
 8         }
 9         System.out.println();
10         for (int i=0;i<ac.array1.length;i++){
11             System.out.print(ac.array2[i]+" ");
12         }
13         System.out.println("\n"+"修改數組array1的值,查看array2是否改變");
14         ac.array1[0]=10;//修改數組第一元素
15         for (int i=0;i<ac.array1.length;i++){
16             System.out.print(ac.array1[i]+" ");
17         }
18         System.out.println();
19         for (int i=0;i<ac.array1.length;i++){
20             System.out.print(ac.array2[i]+" ");
21         }
22     }
23 }

這里只是復制了數組的引用,一個數組的改變會影響到另一個數組。

2、使用arraycopy方法復制基本數據類型數組

2.1. arraycopy方法聲明

public static native void arraycopy(Object src,int srcPos,Object dest,int destPos,int lenhgth);

2.2. arraycopy方法參數簡介

src:源數組

srcPos:開始復制的位置,從源數組哪個位置開始復制

dest:目的數組

descPos:將源數組復制到目標數組的起始位置

length:復制多少個源數組中的元素個數

2.2. arraycopy方法演示

 1 public class Arraycopy {
 2     int[] ary1 = new int[]{1, 2, 3, 4, 5, 6};
 3     int[] ary2 = new int[6];
 4     public  void print(int[] array) {
 5         for (int i : array)
 6             System.out.print(i+" ");
 7         System.out.println();
 8     }
 9     /**
10      * 使用arraycopy方法將數組ary1復制給ary2
11      * @param args
12      */
13     public static void main(String[] args) {
14         Arraycopy ac = new Arraycopy();
15         System.out.println("---兩數組初始---");
16         ac.print(ac.ary1);
17         ac.print(ac.ary2);
18         System.out.println("---將ary1復制給ary2---");
19         System.arraycopy(ac.ary1,0,ac.ary2,0,ac.ary1.length);
20         ac.print(ac.ary1);
21         ac.print(ac.ary2);
22         System.out.println("---修改ary1(ary2)查看ary2(ary1)是否變化---");
23         ac.ary1[0]=0;
24         ac.print(ac.ary1);
25         ac.print(ac.ary2);
26     }
27 }

使用arraycopy方法進行數組復制,就不存在數組的引用,即:一個數組的內容的改變不會影響另一個數組的內容。

3、對象類型(引用類型)數組的復制

 1 import java.awt.*;
 2 
 3 public class Arraycopy2 {
 4     /**
 5      * 定義對象數組
 6      */
 7     Label lb1[] = new Label[]{
 8             new Label("Label1"),
 9             new Label("Label2"),
10             new Label("Label3")
11     };
12     Label lb2[] = new Label[lb1.length];
13 
14     public static void main(String args[]) {
15         Arraycopy2 ac = new Arraycopy2();
16         System.out.println("--將對象數組lb1復制給lb2--");
17         System.out.print("lb1數組:");
18         ac.print(ac.lb1);
19         //ac.lb2 = ac.lb1;
20         System.arraycopy(ac.lb1,0,ac.lb2,0,ac.lb1.length);
21         System.out.print("lb2數組:");
22         ac.print(ac.lb2);
23         System.out.println("\n--修改lb1(lb2)查看lb2(lb1)是否變化--");
24         ac.lb1[0].setText("Label0");
25         System.out.print("lb1數組:");
26         ac.print(ac.lb1);
27         System.out.print("lb2數組:");
28         ac.print(ac.lb2);//修改lb1數組內容后發現lb2數組也發生改變,說明lb1和lb2指向同一內存空間。
29         //當然修改lb2內容lb1內容也會改變,這里不贅述了
30     }
31 
32     public void print(Label[] lb) {
33         for (Label i : lb) {
34             System.out.print(i.getText() + " ");
35         }
36         System.out.println();
37     }
38 }

和基本類型數組是不同的,當數組類型為對象類型時,用arraycopy方法復制數組復制的也只是引用,不是對象本身。

關聯博客(CSDN):https://blog.csdn.net/m0_38022608/article/details/80262416


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM