[JAVA] 가변개수 인자

12·2026년 3월 5일

자바

목록 보기
6/8
public class VariableLengthDemo {
	public static void main(String[] args) {
		//오버로딩을 계속해야한다면?
		change("a");
		change("a", "b");
		change("a", "b", "c");
		change("a", "b", "c", "d");
	}

오버로딩을 계속해야 할 시 가변개수인자로 해결할 수 있다.

	public static void change(String s1) {
		// TODO Auto-generated method stub

	}

	public static void change(String s1, String s2) {
		// TODO Auto-generated method stub

	}

	public static void change(String s1, String s2, String s3) {
		// TODO Auto-generated method stub

	}

	public static void change(String s1, String s2, String s3, String s4) {
		// TODO Auto-generated method stub

	}

이걸

	public static void change(String... s1) { // 가변개수 인자
		for (String s : s1) {
			System.out.print(s + " ");

		}
		System.out.println("");

이렇게

a 
a b 
a b c 
a b c d 
//결과

0개의 댓글