가변인자는 데이터 타입이 한가지일 경우, 인자의 수와 상관없이 하나의 데이터 타입에 복수의 인자를 대입해서 사용할 수 있다.

 

 

1. CLASS

 

import java.util.HashMap;
import java.util.Map;

 

public class t0331_varargs_1051 {
  private static Map<String, String> mo = new HashMap<String, String>();
 
  public static void main(String args[]){
    boolean tf = varArgsTest("A", "B", "C", "D");
  
    if(tf){
      System.out.println("\n" + mo);
      System.out.println(mo.keySet());

      // System.out.println(mo.getClass().cast(mo).values());
      System.out.println(mo.values());
      System.out.println(mo.get("A"));
      System.out.println("key = C, value = " + mo.get("C"));
    }
  }
 
  public static boolean varArgsTest(String... str) {
    boolean bo = str.length > 0 ? true : false;
    String valueC = "AACS";
    int no = str.length;

    for(String key : str){

      // System.out.println("key = " + key.hashCode());
      int num = str.length - no;

 

      if(num <= str.length && num > 0){
        int i = 0;
        String mun = "";


        while(i < valueC.length()){
          char kc = valueC .charAt(i);
          kc++;
          mun += kc + "";
          i++;
        }
        valueC = mun;
      }
      System.out.println(num + ". key = " + key + ",  value = "+ valueC);
      mo.put(key, valueC);
      no--;
    }
    return bo;
  }
}

 

 

2. RUN

 

0. key = A,  value = AACS
1. key = B,  value = BBDT
2. key = C,  value = CCEU
3. key = D,  value = DDFV

 

{A=AACS, B=BBDT, C=CCEU, D=DDFV}
[A, B, C, D]
[AACS, BBDT, CCEU, DDFV]
AACS
key = C, value = CCEU

 

'course > 자바' 카테고리의 다른 글

임의의 수만큼 연속되는 특정문자 치환.  (0) 2017.04.10
연봉/퇴직금 계산  (0) 2017.03.20
지저분한 파일 Move  (0) 2017.02.26