Java複製數組的四種方法:arraycopy()方法、clone() 方法、copyOf()和copyOfRange()方法

所謂複製數組,是指將一個數組中的元素在另一個數組中進行復制。

在 Java 中實現數組複製有 4 種方法,分別爲使用 Arrays 類的 copyOf() 方法和 copyOfRange() 方法、System 類的 arraycopy() 方法和 Object 類的 clone() 方法。下面來詳細介紹這 4 種方法的使用。

使用 copyOf() 方法和 copyOfRange() 方法

Arrays 類的 copyOf() 方法與 copyOfRange() 方法都可實現對數組的複製。copyOf() 方法是複製數組至指定長度,copyOfRange() 方法則將指定數組的指定長度複製到一個新數組中。

1. 使用 copyOf() 方法對數組進行復制

Arrays 類的 copyOf() 方法的語法格式如下:

Arrays.copyOf(dataType[] srcArray,int length);

其中,srcArray 表示要進行復制的數組,length 表示複製後的新數組的長度。

使用這種方法複製數組時,默認從源數組的第一個元素(索引值爲 0)開始複製,目標數組的長度將爲 length。如果 length 大於 srcArray.length,則目標數組中採用默認值填充;如果 length 小於 srcArray.length,則複製到第 length 個元素(索引值爲 length-1)即止。

注意:目標數組如果已經存在,將會被重構。

例 1

假設有一個數組中保存了 5 個成績,現在需要在一個新數組中保存這 5 個成績,同時留 3 個空餘的元素供後期開發使用。

使用 Arrays 類的 CopyOf() 方法完成數組複製的代碼如下:

  1. import java.util.Arrays;
  2. public class Testl9
  3. {
  4. public static void main(String[] args)
  5. {
  6. //定義長度爲 5 的數組
  7. int scores[]=new int[]{57,81,68,75,91};
  8.  
  9. //輸出源數組
  10. System.out.println("源數組內容如下:");
  11.  
  12. //循環遍歷源數組
  13. for(int i=0;i<scores.length;i++)
  14. {
  15. //將數組元素輸出
  16. System.out.print(scores[i]+"\t");
  17. }
  18.  
  19. //定義一個新的數組,將scores數組中的5個元素複製過來
  20. //同時留3個內存空間供以後開發使用
  21. int[] newScores=(int[])Arrays.copyOf(scores,8);
  22. System.out.println("\n複製的新數組內容如下:");
  23.  
  24. //循環遍歷複製後的新數組
  25. for(int j=0;j<newScores.length;j++)
  26. {
  27. //將新數組的元素輸出
  28. System.out.print(newScores[j]+"\t");
  29. }
  30. }
  31. }


在上述代碼中,由於源數組 scores 的長度爲 5,而要複製的新數組 newScores 的長度爲 8,因此在將源數組中的 5 個元素複製完之後,會採用默認值填充剩餘 3 個元素的內容。

因爲源數組 scores 的數據類型爲 int,而使用 Arrays.copyOf(scores,8) 方法複製數組之後返回的是 Object[] 類型,因此需要將 Object[] 數據類型強制轉換爲 int[] 類型。同時,也正因爲 scores 的數據類型爲 int,因此默認值爲 0。

運行的結果如下所示。

源數組內容如下:
57    81    68    75    91   
複製的新數組內容如下:
57    81    68    75    91    0    0    0

2. 使用 CopyOfRange() 方法對數組進行復制

Arrays 類的 CopyOfRange() 方法是另一種複製數組的方法,其語法形式如下:

Arrays.copyOfRange(dataType[] srcArray,int startIndex,int endIndex)


其中,srcArray 表示源數組;startIndex 表示開始複製的起始索引,目標數組中將包含起始索引對應的元素,另外,startIndex 必須在 0 到 srcArray.length 之間;endIndex 表示終止索引,目標數組中將不包含終止索引對應的元素,endIndex 必須大於等於 startIndex,可以大於 srcArray.length,如果大於 srcArray.length,則目標數組中使用默認值填充。

注意:目標數組如果已經存在,將會被重構。

例 2

假設有一個名稱爲 scores 的數組其元素爲 8 個,現在需要定義一個名稱爲 newScores 的新數組。新數組的元素爲 scores 數組的前 5 個元素,並且順序不變。

使用 Arrays 類 copyOfRange() 方法完成數組複製的代碼如下:

  1. import java. util. Arrays;
  2. public class Test20
  3. {
  4. public static void main(String[] args)
  5. {
  6. //定義長度爲8的數組
  7. int scores[]=new int[]{57,81,68,75,91,66,75,84};
  8. System.out.println("源數組內容如下:");
  9.  
  10. //循環遍歷源數組
  11. for(int i=0;i<scores.length;i++)
  12. {
  13. System.out.print(scores[i]+"\t");
  14. }
  15.  
  16. //複製源數組的前5個元素到newScores數組中
  17. int newScores[]=(int[])Arrays.copyOfRange(scores,0,5);
  18. System.out.println("\n複製的新數組內容如下:");
  19.  
  20. //循環遍歷目標數組,即複製後的新數組
  21. for(int j=0;j<newScores.length;j++)
  22. {
  23. System.out.print(newScores[j]+"\t");
  24. }
  25. }

}

在上述代碼中,源數組 scores 中包含有 8 個元素,使用 Arrays.copyOfRange() 方法可以將該數組複製到長度爲 5 的 newScores 數組中,截取 scores 數組的前 5 個元素即可。

該程序運行結果如下所示。

源數組內容如下:
57    81    68    75    91    66    75    84   
複製的新數組內容如下:
57    81    68    75    91

使用 arraycopy() 方法

arraycopy() 方法位於 java.lang.System 類中,其語法形式如下:

System.arraycopy(dataType[] srcArray,int srcIndex,int destArray,int destIndex,int length)


其中,srcArray 表示源數組;srcIndex 表示源數組中的起始索引;destArray 表示目標數組;destIndex 表示目標數組中的起始索引;length 表示要複製的數組長度。

使用此方法複製數組時,length+srcIndex 必須小於等於 srcArray.length,同時 length+destIndex 必須小於等於 destArray.length。

注意:目標數組必須已經存在,且不會被重構,相當於替換目標數組中的部分元素。

例 3

假設在 scores 數組中保存了 8 名學生的成績信息,現在需要複製該數組從第二個元素開始到結尾的所有元素到一個名稱爲 newScores 的數組中,長度爲 12。scores 數組中的元素在 newScores 數組中從第三個元素開始排列。

使用 System.arraycopy() 方法來完成替換數組元素功能的代碼如下:

  1. import java.util.Arrays;
  2. public class Test21
  3. {
  4. public static void main(String[] args)
  5. {
  6. //定義源數組,長度爲8
  7. int scores[]=new int[]{100,81,68,75,91,66,75,100};
  8.  
  9. //定義目標數組
  10. int newScores[]=new int[]{80,82,71,92,68,71,87,88,81,79,90,77};
  11. System.out.println("源數組中的內容如下:");
  12.  
  13. //遍歷源數組
  14. for(int i=0;i<scores.length;i++)
  15. {
  16. System.out.print(scores[i]+"\t");
  17. }
  18. System.out.println("\n目標數組中的內容如下:");
  19.  
  20. //遍歷目標數組
  21. for(int j=0;j<newScores.length;j++)
  22. {
  23. System.out.print(newScores[j]+"\t");
  24. }
  25. System.arraycopy(scores,0,newScores,2,8);
  26.  
  27. //複製源數組中的一部分到目標數組中
  28. System.out.println("\n替換元素後的目標數組內容如下:");
  29.  
  30. //循環遍歷替換後的數組
  31. for(int k=0;k<newScores.length;k++)
  32. {
  33. System.out.print(newScores[k]+"\t");
  34. }
  35. }
  36. }


在該程序中,首先定義了一個包含有 8 個元素的 scores 數組,接着又定義了一個包含有 12 個元素的 newScores 數組,然後使用 for 循環分別遍歷這兩個數組,輸出數組中的元素。最後使用 System.arraycopy() 方法將 newScores 數組中從第三個元素開始往後的 8 個元素替換爲 scores 數組中的 8 個元素值。

該程序運行的結果如下所示。

源數組中的內容如下:
100    81    68    75    91    66    75    100   
目標數組中的內容如下:
80    82    71    92    68    71    87    88    81    79    90    77   
替換元素後的目標數組內容如下:
80    82    100    81    68    75    91    66    75    100    90    77   


注意:在使用 arraycopy() 方法時要注意,此方法的命名違背了 Java 的命名慣例。即第二個單詞 copy 的首字母沒有大寫,但按慣例寫法應該爲 arrayCopy。請讀者在使用此方法時注意方法名的書寫。

使用 clone() 方法

clone() 方法也可以實現複製數組。該方法是類 Object 中的方法,可以創建一個有單獨內存空間的對象。因爲數組也是一個 Object 類,因此也可以使用數組對象的 clone() 方法來複制數組。

clone() 方法的返回值是 Object 類型,要使用強制類型轉換爲適當的類型。其語法形式比較簡單:

array_name.clone()


示例語句如下:

  1. int[] targetArray=(int[])sourceArray.clone();


注意:目標數組如果已經存在,將會被重構。

例 4

有一個長度爲 8 的 scores 數組,因爲程序需要,現在要定義一個名稱爲 newScores 的數組來容納 scores 數組中的所有元素,可以使用 clone() 方法來將 scores 數組中的元素全部複製到 newScores 數組中。代碼如下:

  1. import java.util.Arrays;
  2. public class Test22
  3. {
  4. public static void main(String[] args)
  5. {
  6. //定義源數組,長度爲8
  7. int scores[]=new int[]{100,81,68,75,91,66,75,100};
  8. System.out.println("源數組中的內容如下:");
  9.  
  10. //遍歷源數組
  11. for(int i=0;i<scores.length;i++)
  12. {
  13. System.out.print(scores[i]+"\t");
  14. }
  15.  
  16. //複製數組,將Object類型強制轉換爲int[]類型
  17. int newScores[]=(int[])scores.clone();
  18. System.out.println("\n目標數組內容如下:");
  19.  
  20. //循環遍歷目標數組
  21. for(int k=0;k<newScores.length;k++)
  22. {
  23. System.out.print(newScores[k]+"\t");
  24. }
  25. }
  26. }


在該程序中,首先定義了一個長度爲 8 的 scores 數組,並循環遍歷該數組輸出數組中的元素,然後定義了一個名稱爲 newScores 的新數組,並使用 scores.clone() 方法將 scores 數組中的元素複製給 newScores 數組。最後循環遍歷 newScores 數組,輸出數組元素。

程序運行結果如下所示。

源數組中的內容如下:
100    81    68    75    91    66    75    100   
目標數組內容如下:
100    81    68    75    91    66    75    100   


從運行的結果可以看出,scores 數組的元素與 newScores 數組的元素是相同的。

發佈了41 篇原創文章 · 獲贊 68 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章