Programming

(Java) 일정하지 않은 개수의 파라미터 (Arbitrary Number of Arguments)

steloflute 2013. 7. 3. 23:30

http://haneulnoon.tistory.com/168

 

 

일정한 형의 변수를 여러 개 전달해야 할 때는 아래 소스처럼 해주면 된다. 메소드 호출할 때는 일반 파라미터 처럼 넘기고,

메소드에서 받은 파라미터는 배열로 사용하면 된다.

이제껏 문법이 어려운 것도 아니었는데 몰라서 못썼다.

public class Test{

public static void main(String[] args) {
TestClass tc= new TestClass();

tc.test("test1","test2","test3");

}


}


class TestClass{

public void test(String... strs) {


System.out.println(strs[0]);// test1

System.out.println(strs[1]); // test2

}

}


 


참고 : http://download.oracle.com/javase/tutorial/java/javaOO/arguments.html

 

 

'Programming' 카테고리의 다른 글

UPenn Haskell Course  (0) 2013.07.30
A Taste of the λ Calculus  (0) 2013.07.23
RSA 기반 웹페이지 암호화 로그인  (0) 2013.06.30
정규표현식 (regular expression)  (0) 2013.06.27
(Racket) change encoding  (0) 2013.06.25