Java System.arraycopy() メソッドとは何ですか?
Java System.arraycopy() メソッドの宣言
java.langクラスのjava.lang.System.arraycopy()メソッドのメソッド宣言は次のとおりです。
public static void arraycopy(Object src, int srcIndex, Object dest, int destIndex, int len)
パラメーター
arraycopyメソッドのパラメータは次のとおりです。-
src : ソース配列です。
-
srcIndex : ソース配列の開始インデックスです。
-
dest : 宛先配列です。
-
destIndex : 宛先配列の開始インデックスです。
-
len : コピー元の配列からコピー先の配列にコピーする必要がある要素の数です。
機能性
arraycopyメソッドは、 srcからのデータを、srcIndexからsrcIndex +( len - 1) 要素まで、 destIndexの dest 配列にdestIndex + ( len - 1) までコピーします。戻り値
arraycopyメソッドの戻り値の型はvoidであり、何も返さないことを意味します。例
public class Example {
public static void main(String[] args) {
String[] src = { "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
String[] dest = { "January", "February", "March", "April", "May", "June", "July", "August"};
int srcIndex = 2;
int destIndex = 3;
int len = 2;
//print number of elements that need to be copied
//from the source to the destination array
System.out.println("len : " + len);
//print source index
System.out.println("srcIndex : " + srcIndex);
//print elements of the source array
System.out.print("src : ");
for (int i = 0; i < src.length; i++)
System.out.print(src[i] + " ");
System.out.println("");
//print destination index
System.out.println("destIndex : " + destIndex);
//print elements of the destination array
System.out.print("dest : ");
for (int i = 0; i < dest.length; i++)
System.out.print(dest[i] + " ");
System.out.println("");
// Use of arraycopy() method
System.arraycopy(src, srcIndex, dest, destIndex, len);
// this method copies the 'len' no of elements
// from the src array to the dest array using the srcIndex
// and destIndex as reference points in both the arrays
// Print elements of destination after
System.out.print("final destination array : ");
for (int i = 0; i < dest.length; i++)
System.out.print(dest[i] + " ");
}
}
出力
len : 2 srcIndex : 2 src : 月曜日、火曜日、水曜日、木曜日、金曜日、土曜日、日曜日 destIndex : 3 dest : 1月、2月、3月、4月、5月、6月、7月、8月 最終宛先配列: 1月、2月、3月、水曜日、6月、木曜日、7月、8月
パラメータ変更例
// Example of changing parameters
srcIndex = 4;
destIndex = 5;
len = 1;
出力
len : 1 srcIndex : 4 src : 月曜日、火曜日、水曜日、木曜日、金曜日、土曜日、日曜日 destIndex : 5 dest : 1月、2月、3月、4月、5月、6月、7月、8月 最終宛先配列: 1月、2月、3月、4月、5月、金曜日、7月、8月
GO TO FULL VERSION