所謂復制數組,是指將一個數組中的元素在另一個數組中進行復制。
在 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() 方法完成數組復制的代碼如下:
- import java.util.Arrays;
- public class Testl9
- {
- public static void main(String[] args)
- {
- //定義長度為 5 的數組
- int scores[]=new int[]{57,81,68,75,91};
- //輸出源數組
- System.out.println("源數組內容如下:");
- //循環遍歷源數組
- for(int i=0;i<scores.length;i++)
- {
- //將數組元素輸出
- System.out.print(scores[i]+"\t");
- }
- //定義一個新的數組,將scores數組中的5個元素復制過來
- //同時留3個內存空間供以后開發使用
- int[] newScores=(int[])Arrays.copyOf(scores,8);
- System.out.println("\n復制的新數組內容如下:");
- //循環遍歷復制后的新數組
- for(int j=0;j<newScores.length;j++)
- {
- //將新數組的元素輸出
- System.out.print(newScores[j]+"\t");
- }
- }
- }
在上述代碼中,由於源數組 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() 方法完成數組復制的代碼如下:
- import java. util. Arrays;
- public class Test20
- {
- public static void main(String[] args)
- {
- //定義長度為8的數組
- int scores[]=new int[]{57,81,68,75,91,66,75,84};
- System.out.println("源數組內容如下:");
- //循環遍歷源數組
- for(int i=0;i<scores.length;i++)
- {
- System.out.print(scores[i]+"\t");
- }
- //復制源數組的前5個元素到newScores數組中
- int newScores[]=(int[])Arrays.copyOfRange(scores,0,5);
- System.out.println("\n復制的新數組內容如下:");
- //循環遍歷目標數組,即復制后的新數組
- for(int j=0;j<newScores.length;j++)
- {
- System.out.print(newScores[j]+"\t");
- }
- }
}
在上述代碼中,源數組 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() 方法來完成替換數組元素功能的代碼如下:
- import java.util.Arrays;
- public class Test21
- {
- public static void main(String[] args)
- {
- //定義源數組,長度為8
- int scores[]=new int[]{100,81,68,75,91,66,75,100};
- //定義目標數組
- int newScores[]=new int[]{80,82,71,92,68,71,87,88,81,79,90,77};
- System.out.println("源數組中的內容如下:");
- //遍歷源數組
- for(int i=0;i<scores.length;i++)
- {
- System.out.print(scores[i]+"\t");
- }
- System.out.println("\n目標數組中的內容如下:");
- //遍歷目標數組
- for(int j=0;j<newScores.length;j++)
- {
- System.out.print(newScores[j]+"\t");
- }
- System.arraycopy(scores,0,newScores,2,8);
- //復制源數組中的一部分到目標數組中
- System.out.println("\n替換元素后的目標數組內容如下:");
- //循環遍歷替換后的數組
- for(int k=0;k<newScores.length;k++)
- {
- System.out.print(newScores[k]+"\t");
- }
- }
- }
在該程序中,首先定義了一個包含有 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()
示例語句如下:
- int[] targetArray=(int[])sourceArray.clone();
注意:目標數組如果已經存在,將會被重構。
例 4
有一個長度為 8 的 scores 數組,因為程序需要,現在要定義一個名稱為 newScores 的數組來容納 scores 數組中的所有元素,可以使用 clone() 方法來將 scores 數組中的元素全部復制到 newScores 數組中。代碼如下:
- import java.util.Arrays;
- public class Test22
- {
- public static void main(String[] args)
- {
- //定義源數組,長度為8
- int scores[]=new int[]{100,81,68,75,91,66,75,100};
- System.out.println("源數組中的內容如下:");
- //遍歷源數組
- for(int i=0;i<scores.length;i++)
- {
- System.out.print(scores[i]+"\t");
- }
- //復制數組,將Object類型強制轉換為int[]類型
- int newScores[]=(int[])scores.clone();
- System.out.println("\n目標數組內容如下:");
- //循環遍歷目標數組
- for(int k=0;k<newScores.length;k++)
- {
- System.out.print(newScores[k]+"\t");
- }
- }
- }
在該程序中,首先定義了一個長度為 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 數組的元素是相同的。