728x90
반응형

public void given(String message, Object... args) {

....
}

이건 어디서 어떻게 쓰는 표현인고?

ellipsis (...) identifies a variable number of arguments, and is demonstrated in the following summation method.
ellipsis 라고 불리는 녀석인 것 같다.
이녀석의 특징은 메소드 등에서 동일한 객체의 파라메터(실행에 필요한 변수, 설정변수?)들을 처리할 때, 메소드마다 파라메터의 갯수를 늘려가며 설정하는 대신,
public void method(Int... args) {}  

로 설정해두면, int 형의 파라메터를 몇개를 받아도 처리가 가능하다.

간단정리 : Variable-Length Argument Lists

이 녀석의 정체를 파악해보기 위한 간단한 테스트 코드도 작성해본다.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;
    }
     
}
728x90
반응형

'JAVA > JAVA노하우정보' 카테고리의 다른 글

Java: enum의 뿌리를 찾아서...  (0) 2018.02.08
Java 에서 싱글톤(Singleton) 구현 방법  (0) 2016.03.30
자바 메모리 옵션  (0) 2016.01.26
블로그 이미지

nineDeveloper

안녕하세요 현직 개발자 입니다 ~ 빠르게 변화하는 세상에 뒤쳐지지 않도록 우리모두 열심히 공부합시다 ~! 개발공부는 넘나 재미있는 것~!

,