CodeGym /Java Blog /๋ฌด์ž‘์œ„์˜ /์ž๋ฐ” System.arraycopy() ๋ฉ”์„œ๋“œ
John Squirrels
๋ ˆ๋ฒจ 41
San Francisco

์ž๋ฐ” System.arraycopy() ๋ฉ”์„œ๋“œ

๋ฌด์ž‘์œ„์˜ ๊ทธ๋ฃน์— ๊ฒŒ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค

Java System.arraycopy() ๋ฉ”์†Œ๋“œ๋ž€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

java.lang.System.arraycopy() ๋ฉ”์„œ๋“œ๋Š” ํŠน์ • ์‹œ์ž‘ ์œ„์น˜์˜ ์†Œ์Šค ๋ฐฐ์—ด์„ ์ง€์ •๋œ ์ธ๋ฑ์Šค์˜ ๋Œ€์ƒ์œผ๋กœ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.
์ด ๋ฉ”์†Œ๋“œ๋Š” java.lang.System ํด๋ž˜์Šค ์— ์†ํ•ฉ๋‹ˆ๋‹ค . ์ง€์ •๋œ ์†Œ์Šค ๋ฐฐ์—ด์—์„œ ๋Œ€์ƒ ๋ฐฐ์—ด์ด๋ผ๋Š” ๋‹ค๋ฅธ ๋ฐฐ์—ด๋กœ ์ง€์ •๋œ ๊ธธ์ด ์˜ ํ•˜์œ„ ๋ฐฐ์—ด ๋‚ด์šฉ์„ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค . ์†Œ์Šค ๋ฐฐ์—ด์€ src ๋ผ๊ณ  ํ•˜๊ณ  ๋Œ€์ƒ ๋ฐฐ์—ด์€ dest ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค . ์š”์†Œ์˜ ์ˆ˜๋Š” len ์ธ์ˆ˜๋กœ ์ง€์ •๋ฉ๋‹ˆ๋‹ค.

Java System.arraycopy() ๋ฉ”์„œ๋“œ ์„ ์–ธ

java.lang ํด๋ž˜์Šค์˜ java.lang.System.arraycopy() ๋ฉ”์„œ๋“œ ์— ๋Œ€ํ•œ ๋ฉ”์„œ๋“œ ์„ ์–ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

public static void arraycopy(Object src, int srcIndex, Object dest, int destIndex, int len)

๋งค๊ฐœ๋ณ€์ˆ˜

๋‹ค์Œ์€ arraycopy ๋ฉ”์„œ๋“œ ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค .
  1. src : ์†Œ์Šค ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.

  2. srcIndex : ์†Œ์Šค ๋ฐฐ์—ด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค์ž…๋‹ˆ๋‹ค.

  3. dest : ๋Œ€์ƒ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.

  4. destIndex : ๋Œ€์ƒ ๋ฐฐ์—ด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค์ž…๋‹ˆ๋‹ค.

  5. 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์›”

์„ค๋ช…

์œ„์˜ ์ฝ”๋“œ์—์„œ ๋‘ ๊ฐœ์˜ ๋ฐฐ์—ด src[] ๋ฐ dest[] ๋ฅผ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค . ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋‘ ์–ด๋ ˆ์ด์˜ ์ฐธ์กฐ ์œ„์น˜๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด srcIndex ๋ฐ destIndex๋ฅผ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค . ์ฒซ ๋ฒˆ์งธ ์˜ˆ์—์„œ System.arraycopy ๋ฉ”์„œ๋“œ๋Š” ์ธ๋ฑ์Šค 2์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์ธ๋ฑ์Šค 3์—์„œ ๋๋‚˜๋Š” ์†Œ์Šค ๋ฐฐ์—ด src[] ์˜ ์š”์†Œ ( len = 2์ด๋ฏ€๋กœ)๋ฅผ ์ธ๋ฑ์Šค 3์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๋Œ€์ƒ ๋ฐฐ์—ด๋กœ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋‹ค์Œ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์š”์†Œ๋ฅผ ๋ณต์‚ฌํ•œ ํ›„ ์ตœ์ข… ๋Œ€์ƒ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์˜ˆ์—์„œ System.arraycopy ๋ฉ”์„œ๋“œ ๋Š” ์†Œ์Šค ๋ฐฐ์—ด์—์„œ 1๊ฐœ์˜ ์š”์†Œ( len = 1์ด๊ธฐ ๋•Œ๋ฌธ์—)๋ฅผ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.src[] , ์ธ๋ฑ์Šค 4๋ถ€ํ„ฐ ๋Œ€์ƒ ๋ฐฐ์—ด, ์ธ๋ฑ์Šค 5๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์š”์†Œ ๋ณต์‚ฌ ํ›„ ์ตœ์ข… ๋Œ€์ƒ ๋ฐฐ์—ด๋„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

์ด ๊ฒŒ์‹œ๋ฌผ์ด ๋๋‚  ๋•Œ๊นŒ์ง€ Java์˜ java.lang.System.arraycopy ๋ฉ”์„œ๋“œ ์— ์ต์ˆ™ํ•ด์กŒ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค . ๊ฐœ๋…์˜ ๋” ๊นŠ์€ ๋ช…๋ น์„ ์œ„ํ•ด ๊ณ„์† ์—ฐ์Šตํ•˜์‹ญ์‹œ์˜ค. ๊ทธ๋•Œ๊นŒ์ง€ ๊ณ„์† ์„ฑ์žฅํ•˜๊ณ  ๊ณ„์† ๋น›๋‚˜์‹ญ์‹œ์˜ค!
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION