본문 바로가기

java

자바 가변인자 (variable argument type)

1. 가변인자로 전달되는 인자의 타입은 같아야 됩니다.

2. 메소드 정의시 가변인자는 제일 마지막에 정의되어야 합니다. The variable argument type String of the method testVarArgs must be the last parameter라고 컴파일 에러 메세지가 나오네요.

3. 자바 5 이상에서 동작합니다.


public class Test {

            public static void main(String[] args) {

                     testVarArgs(1, "Microsoft", "Apple", "IBM");

                     testVarArgs(2, "Red", "Green", "Blue", "Black", "White", "Orange", "Yellow");

            }

            public static void testVarArgs(int index, String... strings) {

                     System.out.println("index : " +index);

                     System.out.println("length : " +strings.length);

                     System.out.println("---------------------");

                      for (String str : strings) {

                                System.out.println(str);

                     }

            }

 }


'java' 카테고리의 다른 글

java request information  (0) 2014.01.28
구글날씨 api 이용하기  (5) 2009.01.23
Tag Library Function  (0) 2009.01.19
Custom Tags in JSP Pages  (0) 2008.12.31
Java -classpath 옵션  (1) 2008.04.29