๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Java] ๊ฐ€๋ณ€ ์ธ์ž Varargs

์ถœ์ฒ˜

ChatGPT


๊ฐ€๋ณ€ ์ธ์ž(Varargs)๋ž€

  • ๊ฐ€๋ณ€ ์ธ์ž(Variable-length arguments)๋Š” Java 5๋ถ€ํ„ฐ ๋„์ž…๋œ ๊ธฐ๋Šฅ์œผ๋กœ, ๋ฉ”์†Œ๋“œ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.
  • ๊ฐ€๋ณ€ ์ธ์ž๋Š” ๋ฐฐ์—ด๋กœ ์ฒ˜๋ฆฌ๋˜๋ฉฐ, ๋ฉ”์†Œ๋“œ ๋‚ด์—์„œ ํ•ด๋‹น ์ธ์ž๋Š” ๋ฐฐ์—ด์ฒ˜๋Ÿผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค.

๊ฐ€๋ณ€ ์ธ์ž์˜ ๋ฌธ๋ฒ•

๊ฐ€๋ณ€ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฉ”์†Œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž… ๋’ค์— '...'๋ฅผ ๋ถ™์ด๋ฉด ๋œ๋‹ค.

 

์˜ˆ

public class VarargsExample {
    public static void main(String[] args) {
        printNumbers(1, 2, 3);
        printNumbers(4, 5, 6, 7, 8);
        printNumbers(); // ์ธ์ž๊ฐ€ ์—†์„ ๋•Œ๋„ ํ˜ธ์ถœ ๊ฐ€๋Šฅ
    }

    public static void printNumbers(int... numbers) {
        for (int number : numbers) {
            System.out.print(number + " ");
        }
        System.out.println();
    }
}

 

์˜ˆ์ œ ์„ค๋ช…

 

  • printNumbers(int... numbers) ๋ฉ”์†Œ๋“œ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ int ๊ฐ’์„ ์ธ์ž๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
  • numbers๋Š” ๋ฐฐ์—ด์ฒ˜๋Ÿผ ๋‹ค๋ฃจ์–ด์ง€๋ฉฐ, ๋ฉ”์†Œ๋“œ ๋‚ด๋ถ€์—์„œ ๋ฐ˜๋ณต๋ฌธ ๋“ฑ์„ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต.
  • ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ์‹œ, ์›ํ•˜๋Š” ๊ฐœ์ˆ˜์˜ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์•„๋ฌด ์ธ์ž๋„ ์ „๋‹ฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

๋”๋ณด๊ธฐ
 

์ž๋ฐ”์—์„œ ๊ฐ€๋ณ€ ์ธ์ž(Varargs)๋กœ ์„ ์–ธ๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ธ์ž ์—†์ด ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋‚ด๋ถ€์ ์œผ๋กœ ๊ฐ€๋ณ€ ์ธ์ž๋Š” ๊ธธ์ด๊ฐ€ 0์ธ ๋ฐฐ์—ด๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ด๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์œผ๋ฉฐ, ๊ฐ€๋ณ€ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์ด ์ƒํ™ฉ์„ ๊ณ ๋ คํ•˜์—ฌ ์„ค๊ณ„๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

ํ•˜์ง€๋งŒ, ๋งŒ์•ฝ ๋ฉ”์†Œ๋“œ ๋‚ด์—์„œ ๊ฐ€๋ณ€ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, null ์ฒดํฌ ์—†์ด ๋ฐฐ์—ด์„ ์ฐธ์กฐํ•˜๋ฉด NullPointerException์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€๋ณ€ ์ธ์ž๋กœ ๋ฐฐ์—ด์„ ์ „๋‹ฌํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ๋น„์–ด ์žˆ๋Š” ๋ฐฐ์—ด์ด ์ „๋‹ฌ๋˜๋ฏ€๋กœ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์ง€๋งŒ, ๋งŒ์•ฝ ์˜๋„์ ์œผ๋กœ null์„ ์ „๋‹ฌํ•œ๋‹ค๋ฉด ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.

 

public static void processNumbers(int... numbers) {
    // numbers.length๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ญ์ƒ ์•ˆ์ „ํ•จ
    for (int number : numbers) {  // numbers๊ฐ€ null์ผ ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ฐœ์ƒ
        System.out.println(number);
    }
}

public static void main(String[] args) {
    processNumbers();  // ์•ˆ์ „ํ•˜๊ฒŒ ํ˜ธ์ถœ ๊ฐ€๋Šฅ
    // processNumbers(null); // NullPointerException ๋ฐœ์ƒ
}

 

 

๊ฐ€๋ณ€ ์ธ์ž ์‚ฌ์šฉ์˜ ์žฅ์ 

์ฝ”๋“œ ๊ฐ„๊ฒฐํ™”

๊ฐ€๋ณ€ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ์˜ค๋ฒ„๋กœ๋“œ๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ ๋„ ๋‹ค์–‘ํ•œ ์ธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์œ ์—ฐ์„ฑ

ํ˜ธ์ถœํ•˜๋Š” ์ธก์—์„œ ์ธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์œ ์—ฐํ•˜๊ฒŒ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์–ด API ์‚ฌ์šฉ์ด ํŽธ๋ฆฌํ•ด์ง„๋‹ค.

๋ช…๋ฃŒํ•จ

๋ฐฐ์—ด์„ ๋ช…์‹œ์ ์œผ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ ๋„ ์—ฌ๋Ÿฌ ์ธ์ž๋ฅผ ์‰ฝ๊ฒŒ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๊ฐ€๋ณ€ ์ธ์ž์˜ ์ œ์•ฝ์‚ฌํ•ญ

๊ฐ€๋ณ€ ์ธ์ž๋Š” ๋งˆ์ง€๋ง‰ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

๋ฉ”์†Œ๋“œ ์‹œ๊ทธ๋‹ˆ์ฒ˜์—์„œ ๊ฐ€๋ณ€ ์ธ์ž๋Š” ํ•ญ์ƒ ๋งˆ์ง€๋ง‰์— ์™€์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 'public void example(int a, String... args)'์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์˜ค๋ฒ„๋กœ๋”ฉ ๋ชจํ˜ธ์„ฑ

๊ฐ€๋ณ€ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์˜ค๋ฒ„๋กœ๋”ฉ๋œ ๋ฉ”์†Œ๋“œ๊ฐ€ ๋งŽ๋‹ค๋ฉด ๋ชจํ˜ธ์„ฑ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์–ด๋–ค ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ• ์ง€ ๊ฒฐ์ •ํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ๋‹ค.

ํผํฌ๋จผ์Šค ๊ณ ๋ ค

๊ฐ€๋ณ€ ์ธ์ž๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋งŽ์€ ์ธ์ž๊ฐ€ ์ž์ฃผ ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ ํผํฌ๋จผ์Šค์— ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜ ์žˆ๋‹ค.

 

๊ฐ€๋ณ€ ์ธ์ž์˜ ์‹ค์šฉ์  ์‚ฌ์šฉ ์˜ˆ

๊ฐ€๋ณ€ ์ธ์ž๋Š” ์ฃผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์— ์œ ์šฉํ•˜๋‹ค.

1. ๋กœ๊ทธ ๋ฉ”์†Œ๋“œ

๋‹ค์–‘ํ•œ ๊ฐœ์ˆ˜์˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋กœ๊น… ๋ฉ”์†Œ๋“œ

public void log(String level, String... messages) {
    for (String message : messages) {
        System.out.println(level + ": " + message);
    }
}

 

2.์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ฉ”์†Œ๋“œ

์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ฉ”์†Œ๋“œ

public static int sum(int... values) {
    int total = 0;
    for (int value : values) {
        total += value;
    }
    return total;
}

 

3. ํฌ๋งทํŒ… ๋ฉ”์†Œ๋“œ

๋‹ค์–‘ํ•œ ๊ธธ์ด์˜ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํฌ๋งทํŒ… ๋ฉ”์†Œ๋“œ

public static String format(String template, Object... args) {
    return String.format(template, args);
}

 

๊ฐ€๋ณ€ ์ธ์ž์™€ ๋ฐฐ์—ด์˜ ์ฐจ์ด์ 

 

  • ๋ฐฐ์—ด์€ ๊ณ ์ •๋œ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋ฉฐ, ๋ช…์‹œ์ ์œผ๋กœ ์ƒ์„ฑ๋˜์–ด์•ผ ํ•œ๋‹ค.
  • ๊ฐ€๋ณ€ ์ธ์ž๋Š” ์ธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ช…์‹œํ•˜์ง€ ์•Š๊ณ ๋„ ๋‹ค์–‘ํ•œ ๊ฐœ์ˆ˜์˜ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์˜ˆ์‹œ ์ฝ”๋“œ ๋น„๊ต

๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•œ ๋ฉ”์†Œ๋“œ

public static void printArray(int[] numbers) {
    for (int number : numbers) {
        System.out.print(number + " ");
    }
    System.out.println();
}

public static void main(String[] args) {
    printArray(new int[]{1, 2, 3});
}

 

 

๊ฐ€๋ณ€ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•œ ๋ฉ”์†Œ๋“œ

public static void printVarargs(int... numbers) {
    for (int number : numbers) {
        System.out.print(number + " ");
    }
    System.out.println();
}

public static void main(String[] args) {
    printVarargs(1, 2, 3);
}

 

๊ฒฐ๋ก 

๊ฐ€๋ณ€ ์ธ์ž('ํƒ€์ž…... ๋ณ€์ˆ˜๋ช…')๋Š” Java์—์„œ ๋‹ค์–‘ํ•œ ์ˆ˜์˜ ์ธ์ž๋ฅผ ๋ฉ”์†Œ๋“œ๋กœ ์ „๋‹ฌํ•  ๋•Œ ๋งค์šฐ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ์ด๋‹ค. ์ฝ”๋“œ๋ฅผ ๋” ๊ฐ„๊ฒฐํ•˜๊ณ  ์œ ์—ฐํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ์ฃผ๋ฉฐ, ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ, ํผํฌ๋จผ์Šค์™€ ์˜ค๋ฒ„๋กœ๋”ฉ ๋ชจํ˜ธ์„ฑ์— ์ฃผ์˜ํ•˜์—ฌ ์ ์ ˆํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.


ChatGPT ๊ธ€์˜ ์ถœ์ฒ˜

์ถœ์ฒ˜:

 

Arbitrary Number of Arguments
์ž„์˜์˜ ์ธ์ˆ˜ ์ˆ˜

You can use a construct called varargs to pass an arbitrary number of values to a method. You use varargs when you don't know how many of a particular type of argument will be passed to the method. It's a shortcut to creating an array manually (the previous method could have used varargs rather than an array).
varargs๋ผ๋Š” ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž„์˜ ๊ฐœ์ˆ˜์˜ ๊ฐ’์„ ๋ฉ”์†Œ๋“œ์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ์œ ํ˜•์˜ ์ธ์ˆ˜ ์ค‘ ๋ช‡ ๊ฐœ๊ฐ€ ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌ๋ ์ง€ ์•Œ ์ˆ˜ ์—†์„ ๋•Œ ๊ฐ€๋ณ€ ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ˆ˜๋™์œผ๋กœ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š” ์ง€๋ฆ„๊ธธ์ž…๋‹ˆ๋‹ค(์ด์ „ ๋ฐฉ๋ฒ•์—์„œ๋Š” ๋ฐฐ์—ด ๋Œ€์‹  ๊ฐ€๋ณ€ ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค).

To use varargs, you follow the type of the last parameter by an ellipsis (three dots, ...), then a space, and the parameter name. The method can then be called with any number of that parameter, including none.

๊ฐ€๋ณ€ ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋งˆ์ง€๋ง‰ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์œ ํ˜• ๋’ค์— ์ค„์ž„ํ‘œ(์  3๊ฐœ, ...), ๊ณต๋ฐฑ, ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์ฐจ๋ก€๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์—†์Œ์„ ํฌํ•จํ•˜์—ฌ ํ•ด๋‹น ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์›ํ•˜๋Š” ๋งŒํผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.