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;
}
}
'Java > Language' 카테고리의 다른 글
| Collection 및 Map 인터페이스의 이해 (0) | 2011.05.30 |
|---|---|
| 제 11회 2011 한국자바개발자컨퍼런스 (0) | 2011.05.20 |
| java.io.Serializable , 직렬화, 멀티쓰레드에 사용되는 도메인 객체에 선언된다. (3) | 2011.05.04 |
| Java 에서 싱글톤(Singleton) 구현 방법 (0) | 2011.03.18 |
| Java Archive : 자바 관련 창고 (1) | 2011.03.10 |