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 메서드는 srcIndex 에서 시작하여 srcIndex +( len - 1) 요소 까지 src에서 데이터를 destIndex 에서 destIndex + ( len - 1) 까지 dest 배열로 복사합니다 .반환 값
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