Java/Language
Parameter... 표현(동일한 파라메터를 여러개 받을 때, 자동으로 배열처리)
허니몬
2011. 5. 9. 10:42
public void given(String message, Object... args) {....}
이건 어디서 어떻게 쓰는 표현인고?
ellipsis (...) identifies a variable number of arguments, and is demonstrated in the following summation method.
ellipsis 라고 불리는 녀석인 것 같다.
이녀석의 특징은 메소드 등에서 동일한 객체의 파라메터(실행에 필요한 변수, 설정변수?)들을 처리할 때, 메소드마다 파라메터의 갯수를 늘려가며 설정하는 대신,
로 설정해두면, int 형의 파라메터를 몇개를 받아도 처리가 가능하다.
이 녀석의 정체를 파악해보기 위한 간단한 테스트 코드도 작성해본다.
ellipsis 라고 불리는 녀석인 것 같다.
이녀석의 특징은 메소드 등에서 동일한 객체의 파라메터(실행에 필요한 변수, 설정변수?)들을 처리할 때, 메소드마다 파라메터의 갯수를 늘려가며 설정하는 대신,
public void method(Int... args) {}
로 설정해두면, int 형의 파라메터를 몇개를 받아도 처리가 가능하다.
간단정리 : Variable-Length Argument Lists
이 녀석의 정체를 파악해보기 위한 간단한 테스트 코드도 작성해본다.package honeymon.java.study; import org.junit.Test; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; public class TestEllipsis { @Test public void testEllipsis() { assertThat(lengthEllipsis(3, 4, 5, 6), is(4)); assertThat(countEllipsis(2, 3, 4, 5, 6), is(20)); assertThat(stringEllipsis("Korea", "Japan", "China"), is("Korea is Strong country.")); } private String stringEllipsis(String...national) { String stmt = null; for (int i = 0; i < national.length; i++ ){ if("Korea".equals(national[i])) { stmt = national[i] + " is Strong country."; } } return stmt; } private Integer countEllipsis(int... numberArray) { int sumresult = 0; for (int i = 0; i < numberArray.length; i++) { sumresult += numberArray[i]; } return sumresult; } private Integer lengthEllipsis(int... number) { return number.length; } }