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