Java復(fù)制(拷貝)數(shù)組的4種方法:arraycopy()方法、clone() 方法、copyOf()和copyOfRan
所謂復(fù)制數(shù)組,是指將一個數(shù)組中的元素在另一個數(shù)組中進行復(fù)制。本文主要介紹關(guān)于 Java 里面的數(shù)組復(fù)制(拷貝)的幾種方式和用法。
在 Java 中實現(xiàn)數(shù)組復(fù)制分別有以下 4 種方法:
- Arrays 類的 copyOf() 方法
- Arrays 類的 copyOfRange() 方法
- System 類的 arraycopy() 方法
- Object 類的 clone() 方法
下面來詳細介紹這 4 種方法的使用。
使用 copyOf() 方法和 copyOfRange() 方法
Arrays 類的 copyOf() 方法與 copyOfRange() 方法都可實現(xiàn)對數(shù)組的復(fù)制。copyOf() 方法是復(fù)制數(shù)組至指定長度,copyOfRange() 方法則將指定數(shù)組的指定長度復(fù)制到一個新數(shù)組中。
1. 使用 copyOf() 方法對數(shù)組進行復(fù)制
Arrays 類的 copyOf() 方法的語法格式如下:
Arrays.copyOf(dataType[] srcArray,int length);
其中,srcArray 表示要進行復(fù)制的數(shù)組,length 表示復(fù)制后的新數(shù)組的長度。
使用這種方法復(fù)制數(shù)組時,默認從原數(shù)組的第一個元素(索引值為 0)開始復(fù)制,目標數(shù)組的長度將為 length。如果 length 大于 srcArray.length,則目標數(shù)組中采用默認值填充;如果 length 小于 srcArray.length,則復(fù)制到第 length 個元素(索引值為 length-1)即止。
注意:目標數(shù)組如果已經(jīng)存在,將會被重構(gòu)。
例 1
假設(shè)有一個數(shù)組中保存了 5 個成績,現(xiàn)在需要在一個新數(shù)組中保存這 5 個成績,同時留 3 個空余的元素供后期開發(fā)使用。
使用 Arrays 類的 CopyOf() 方法完成數(shù)組復(fù)制的代碼如下:
import java.util.Arrays;
public class Test19{
public static void main(String[] args) {
// 定義長度為 5 的數(shù)組
int scores[] = new int[]{57,81,68,75,91};
// 輸出原數(shù)組
System.out.println("原數(shù)組內(nèi)容如下:");
// 循環(huán)遍歷原數(shù)組
for(int i=0;i<scores.length;i++) {
// 將數(shù)組元素輸出
System.out.print(scores[i]+"\t");
}
// 定義一個新的數(shù)組,將 scores 數(shù)組中的 5 個元素復(fù)制過來
// 同時留 3 個內(nèi)存空間供以后開發(fā)使用
int[] newScores = (int[])Arrays.copyOf(scores,8);
System.out.println("\n復(fù)制的新數(shù)組內(nèi)容如下:");
// 循環(huán)遍歷復(fù)制后的新數(shù)組
for(int j=0;j<newScores.length;j++) {
// 將新數(shù)組的元素輸出
System.out.print(newScores[j]+"\t");
}
}
}
在上述代碼中,由于原數(shù)組 scores 的長度為 5,而要復(fù)制的新數(shù)組 newScores 的長度為 8,因此在將原數(shù)組中的 5 個元素復(fù)制完之后,會采用默認值填充剩余 3 個元素的內(nèi)容。
因為原數(shù)組 scores 的數(shù)據(jù)類型為 int,而使用 Arrays.copyOf(scores,8) 方法復(fù)制數(shù)組之后返回的是 Object[] 類型,因此需要將 Object[] 數(shù)據(jù)類型強制轉(zhuǎn)換為 int[] 類型。同時,也正因為 scores 的數(shù)據(jù)類型為 int,因此默認值為 0。
運行的結(jié)果如下所示。
原數(shù)組內(nèi)容如下:
57 81 68 75 91
復(fù)制的新數(shù)組內(nèi)容如下:
57 81 68 75 91 0 0 0
2. 使用 CopyOfRange() 方法對數(shù)組進行復(fù)制
Arrays 類的 CopyOfRange() 方法是另一種復(fù)制數(shù)組的方法,其語法形式如下:
Arrays.copyOfRange(dataType[] srcArray,int startIndex,int endIndex)
其中:
- srcArray 表示原數(shù)組。
- startIndex 表示開始復(fù)制的起始索引,目標數(shù)組中將包含起始索引對應(yīng)的元素,另外,startIndex 必須在 0 到 srcArray.length 之間。
- endIndex 表示終止索引,目標數(shù)組中將不包含終止索引對應(yīng)的元素,endIndex 必須大于等于 startIndex,可以大于 srcArray.length,如果大于 srcArray.length,則目標數(shù)組中使用默認值填充。
注意:目標數(shù)組如果已經(jīng)存在,將會被重構(gòu)。
例 2
假設(shè)有一個名稱為 scores 的數(shù)組其元素為 8 個,現(xiàn)在需要定義一個名稱為 newScores 的新數(shù)組。新數(shù)組的元素為 scores 數(shù)組的前 5 個元素,并且順序不變。
使用 Arrays 類 copyOfRange() 方法完成數(shù)組復(fù)制的代碼如下:
public class Test20 {
public static void main(String[] args) {
// 定義長度為8的數(shù)組
int scores[] = new int[] { 57, 81, 68, 75, 91, 66, 75, 84 };
System.out.println("原數(shù)組內(nèi)容如下:");
// 循環(huán)遍歷原數(shù)組
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + "\t");
}
// 復(fù)制原數(shù)組的前5個元素到newScores數(shù)組中
int newScores[] = (int[]) Arrays.copyOfRange(scores, 0, 5);
System.out.println("\n復(fù)制的新數(shù)組內(nèi)容如下:");
// 循環(huán)遍歷目標數(shù)組,即復(fù)制后的新數(shù)組
for (int j = 0; j < newScores.length; j++) {
System.out.print(newScores[j] + "\t");
}
}
}
在上述代碼中,原數(shù)組 scores 中包含有 8 個元素,使用 Arrays.copyOfRange() 方法可以將該數(shù)組復(fù)制到長度為 5 的 newScores 數(shù)組中,截取 scores 數(shù)組的前 5 個元素即可。
該程序運行結(jié)果如下所示。
原數(shù)組內(nèi)容如下:
57 81 68 75 91 66 75 84
復(fù)制的新數(shù)組內(nèi)容如下:
57 81 68 75 91
使用 arraycopy() 方法
arraycopy() 方法位于 java.lang.System 類中,其語法形式如下:
System.arraycopy(dataType[] srcArray,int srcIndex,int destArray,int destIndex,int length)
其中,srcArray 表示原數(shù)組;srcIndex 表示原數(shù)組中的起始索引;destArray 表示目標數(shù)組;destIndex 表示目標數(shù)組中的起始索引;length 表示要復(fù)制的數(shù)組長度。
使用此方法復(fù)制數(shù)組時,length+srcIndex 必須小于等于 srcArray.length,同時 length+destIndex 必須小于等于 destArray.length。
注意:目標數(shù)組必須已經(jīng)存在,且不會被重構(gòu),相當于替換目標數(shù)組中的部分元素。
例 3
假設(shè)在 scores 數(shù)組中保存了 8 名學(xué)生的成績信息,現(xiàn)在需要復(fù)制該數(shù)組從第二個元素開始到結(jié)尾的所有元素到一個名稱為 newScores 的數(shù)組中,長度為 12。scores 數(shù)組中的元素在 newScores 數(shù)組中從第三個元素開始排列。
使用 System.arraycopy() 方法來完成替換數(shù)組元素功能的代碼如下:
public class Test21 {
public static void main(String[] args) {
// 定義原數(shù)組,長度為8
int scores[] = new int[] { 100, 81, 68, 75, 91, 66, 75, 100 };
// 定義目標數(shù)組
int newScores[] = new int[] { 80, 82, 71, 92, 68, 71, 87, 88, 81, 79, 90, 77 };
System.out.println("原數(shù)組中的內(nèi)容如下:");
// 遍歷原數(shù)組
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + "\t");
}
System.out.println("\n目標數(shù)組中的內(nèi)容如下:");
// 遍歷目標數(shù)組
for (int j = 0; j < newScores.length; j++) {
System.out.print(newScores[j] + "\t");
}
System.arraycopy(scores, 0, newScores, 2, 8);
// 復(fù)制原數(shù)組中的一部分到目標數(shù)組中
System.out.println("\n替換元素后的目標數(shù)組內(nèi)容如下:");
// 循環(huán)遍歷替換后的數(shù)組
for (int k = 0; k < newScores.length; k++) {
System.out.print(newScores[k] + "\t");
}
}
}
在該程序中,首先定義了一個包含有 8 個元素的 scores 數(shù)組,接著又定義了一個包含有 12 個元素的 newScores 數(shù)組,然后使用 for 循環(huán)分別遍歷這兩個數(shù)組,輸出數(shù)組中的元素。最后使用 System.arraycopy() 方法將 newScores 數(shù)組中從第三個元素開始往后的 8 個元素替換為 scores 數(shù)組中的 8 個元素值。
該程序運行的結(jié)果如下所示。
原數(shù)組中的內(nèi)容如下:
100 81 68 75 91 66 75 100
目標數(shù)組中的內(nèi)容如下:
80 82 71 92 68 71 87 88 81 79 90 77
替換元素后的目標數(shù)組內(nèi)容如下:
80 82 100 81 68 75 91 66 75 100 90 77
注意:在使用 arraycopy() 方法時要注意,此方法的命名違背了 Java 的命名慣例。即第二個單詞 copy 的首字母沒有大寫,但按慣例寫法應(yīng)該為 arrayCopy。請讀者在使用此方法時注意方法名的書寫。
使用 clone() 方法
clone() 方法也可以實現(xiàn)復(fù)制數(shù)組。該方法是類 Object 中的方法,可以創(chuàng)建一個有單獨內(nèi)存空間的對象。因為數(shù)組也是一個 Object 類,因此也可以使用數(shù)組對象的 clone() 方法來復(fù)制數(shù)組。
clone() 方法的返回值是 Object 類型,要使用強制類型轉(zhuǎn)換為適當?shù)念愋汀F湔Z法形式比較簡單:
array_name.clone()
示例語句如下:
int[] targetArray=(int[])sourceArray.clone();
注意:目標數(shù)組如果已經(jīng)存在,將會被重構(gòu)。
例 4
有一個長度為 8 的 scores 數(shù)組,因為程序需要,現(xiàn)在要定義一個名稱為 newScores 的數(shù)組來容納 scores 數(shù)組中的所有元素,可以使用 clone() 方法來將 scores 數(shù)組中的元素全部復(fù)制到 newScores 數(shù)組中。代碼如下:
public class Test22 {
public static void main(String[] args) {
// 定義原數(shù)組,長度為8
int scores[] = new int[] { 100, 81, 68, 75, 91, 66, 75, 100 };
System.out.println("原數(shù)組中的內(nèi)容如下:");
// 遍歷原數(shù)組
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + "\t");
}
// 復(fù)制數(shù)組,將Object類型強制轉(zhuǎn)換為int[]類型
int newScores[] = (int[]) scores.clone();
System.out.println("\n目標數(shù)組內(nèi)容如下:");
// 循環(huán)遍歷目標數(shù)組
for (int k = 0; k < newScores.length; k++) {
System.out.print(newScores[k] + "\t");
}
}
}
在該程序中,首先定義了一個長度為 8 的 scores 數(shù)組,并循環(huán)遍歷該數(shù)組輸出數(shù)組中的元素,然后定義了一個名稱為 newScores 的新數(shù)組,并使用 scores.clone() 方法將 scores 數(shù)組中的元素復(fù)制給 newScores 數(shù)組。最后循環(huán)遍歷 newScores 數(shù)組,輸出數(shù)組元素。
程序運行結(jié)果如下所示。
原數(shù)組中的內(nèi)容如下:
100 81 68 75 91 66 75 100
目標數(shù)組內(nèi)容如下:
100 81 68 75 91 66 75 100
從運行的結(jié)果可以看出,scores 數(shù)組的元素與 newScores 數(shù)組的元素是相同的。
注意:以上幾種方法都是淺拷貝(淺復(fù)制)。淺拷貝只是復(fù)制了對象的引用地址,兩個對象指向同一個內(nèi)存地址,所以修改其中任意的值,另一個值都會隨之變化。深拷貝是將對象及值復(fù)制過來,兩個對象修改其中任意的值另一個值不會改變。
到此這篇關(guān)于Java復(fù)制(拷貝)數(shù)組的4種方法:arraycopy()方法、clone() 方法、copyOf()和copyOfRan的文章就介紹到這了,更多相關(guān)Java復(fù)制數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring boot thymeleaf 圖片上傳web項目根目錄操作步驟
這篇文章主要介紹了spring boot thymeleaf 圖片上傳web項目根目錄步驟,本文給大家提到了thymeleaf的基礎(chǔ)知識,需要的朋友可以參考下2018-03-03
SpringBoot接入輕量級分布式日志框架(GrayLog)的操作方法
這篇文章主要介紹了SpringBoot接入輕量級分布式日志框架(GrayLog)的方法,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03

