Programming Language/Java
자바 매개변수 - 가변인수
Jaybon
2020. 3. 11. 16:19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package myapp;
import java.awt.Color;
import java.awt.Frame;
public class ex3 extends Object {
// 매개변수 자동으로 배열이 된다.
public static void plus(int...arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println(sum);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
plus(1);
plus(1,2);
plus(1,2,3,4);
// 가변인수 - 매개변수 개수가 가변적이다
System.out.printf("aaa", "a", "b");
System.out.printf("aaa", "a", "b", "c");
/*
Frame f = new Frame();
f.setSize(500, 500);
f.setBackground(Color.BLUE);
f.setVisible(true);
*/
}
}
|
cs |
위 코드를 실행하면 아래와 같이 나옵니다.