๊ด€๋ฆฌ ๋ฉ”๋‰ด

It's easy, if you try

[Java] ์ฝ”๋“œ ๊ฐœ์„ ์„ ์œ„ํ•œ Collection ์‚ฌ์šฉ ๋ณธ๋ฌธ

์–ธ์–ด/์ž๋ฐ”(Java)

[Java] ์ฝ”๋“œ ๊ฐœ์„ ์„ ์œ„ํ•œ Collection ์‚ฌ์šฉ

s5he2 2021. 7. 2. 21:46

๐Ÿ‘Š  ์ฝ”๋“œ ๊ฐœ์„ ์„ ์œ„ํ•œ Collection ์‚ฌ์šฉ

๋ชฉ์ฐจ

  1. Collection์˜ ์žฅ๋‹จ์ 
  2. ์‹ค์Šต (1)
  3. ์‹ค์Šต (2)

1. Collection์˜ ์žฅ๋‹จ์ 

์œ„์™€ ๊ฐ™์ด ํด๋ž˜์Šค A์™€ B๊ฐ€ ์žˆ์„ ๋•Œ, ๊ฐ„๊ฒฐ์„ฑ๊ณผ ๊ฐ€๋…์„ฑ๋ฉด์—์„œ๋Š” B๊ฐ€ ์šฐ์ˆ˜ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜, ํž™ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด๋‚˜ ํ•จ์ˆ˜ ์‹คํ–‰ ์†๋„ ๋ฉด์—์„œ๋Š” A๊ฐ€ ๋” ์šฐ์ˆ˜ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ๋”ฐ๋ผ์„œ, ์žฅ/๋‹จ์ ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋Š ๋ฐฉ์‹์„ ์ ์šฉํ• ์ง€๋Š” ์š”๊ตฌ์‚ฌํ•ญ, ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰, ๊ฐ€๋…์„ฑ, CPU ์—ฐ์‚ฐ๋Ÿ‰ ๋“ฑ ๋‹ค๊ฐ๋„๋กœ ๊ณ ๋ คํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

2. ์‹ค์Šต (1)

๐Ÿ“Œswitch ~ case ~ ๋ฌธ์„ ํ†ตํ•ด ์ž‘์„ฑํ•œ ํ˜„์žฌ ๋‹ฌ์˜ ๋‚  ์ˆ˜ ์ถœ๋ ฅ ํ”„๋กœ๊ทธ๋žจ์„ Collection์„ ์‚ฌ์šฉํ•ด ๊ฐœ์„ ํ•ด ๋ด…์‹œ๋‹ค.

๊ฐœ์„  ์ „

import java.util.*;
import java.text.SimpleDateFormat;

public class ์ฝ”๋“œ๊ฐœ์„ ์„_์œ„ํ•œ_Collection_์‚ฌ์šฉ_์œค๋…„ {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String date = sdf.format(new Date());
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(5));
        int day= 0;

        switch(month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 12:
            day = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            day = 30;
            break;
        case 2:
            day = (year % 4) == 0 && (year % 100) != 0 || (year % 400) == 0 ? 29 : 28;
        break;
        }

        System.out.println(day + " days for " + year + "-" + month);
    }
}

๊ฐœ์„  ํ›„

import java.util.*;
import java.text.SimpleDateFormat;

public class ์ฝ”๋“œ๊ฐœ์„ ์„_์œ„ํ•œ_Collection_์‚ฌ์šฉ_์œค๋…„ {
    public static void main(String[] args) {
        int[][] days = {{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
            {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String date = sdf.format(new Date());
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(5));
        int idx = (year % 4) == 0 && (year % 100) != 0 || (year % 400) == 0 ? 0 : 1; 

        System.out.println(days[idx][month] + " days for " + year + "-" + month);
    }
}

์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง„ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3. ์‹ค์Šต (2)

๐Ÿ“Œ ์•ˆ๋“œ๋กœ์ด๋“œ ์ฝ”๋“œ์—์„œ ์ธํ…ํŠธ๋กœ ๋ฐ›์•„์˜จ mode ๊ฐ’๊ณผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜์–ด์˜ค๋Š” result์— ๋”ฐ๋ผ ์ฒ˜๋ฆฌ๊ฐ€ ๋‹ฌ๋ผ์ง€๋Š” ์ฝ”๋“œ๋ฅผ Collection์„ ์‚ฌ์šฉํ•ด ๊ฐœ์„ ํ•ด ๋ด…์‹œ๋‹ค.

๊ฐœ์„  ์ „

์ค‘๋ณต ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ๋ณด์ด๊ณ , ์ง๊ด€์ ์ด์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.

public void returnResult(boolean result) {
  String mode = getIntent().getExtras().getString("mode");
  Intent intent = new Intent();
  switch(mode) {
    case "register": {
      // setResult(resultCode, Intent)
            setResult(1000, intent);
      if (result) {
        // intent.putExtra(name, value);
        intent.putExtra("result", "true"); 
      } else {
        intent.putExtra("result", "false");  
      }
            finish();
    }
    case "auth": {
      setResult(4000, intent);
      if (result) {
          intent.putExtra("result","true");
      } else {
          intent.putExtra("result","true");
      }
      finish();
    }
  }
}

์œ„ ์ฝ”๋“œ์—์„œ๋Š” mode์— ๋”ฐ๋ผ resultCode๊ฐ€ ๋‹ฌ๋ผ์ง€๊ณ ,

ํŒŒ๋ผ๋ฏธํ„ฐ์ธ result ๊ฐ’(boolean)์— ๋”ฐ๋ผ value๊ฐ€ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ ๊ณผ Collection์„ ํ™œ์šฉํ•ด ์ฝ”๋“œ๋ฅผ ๊ฐœ์„ ํ•ด ๋ด…์‹œ๋‹ค!

๊ฐœ์„  ํ›„

public void returnResult(boolean result) {
    Intent intent = new Intent();
      String intentValue = result ? "true" : "false";
    HashMap<String, Integer> resultCode = new HashMap<String, Integer>() 
    {{
          // put(mode, resultCode)
         put("register", 1000);
         put("auth", 4000);
    }};

    setResult(resultCode.get(mode), intent);
    intent.putExtra("result", intentValue);
    finish();
}

HashMap์„ ์‚ฌ์šฉํ•ด mode๋ฅผ ํ‚ค ๊ฐ’์œผ๋กœ, resultCode๋ฅผ ๋ฐธ๋ฅ˜ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. resultCode.get(mode) ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— switch๋ฌธ์„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์–ด์กŒ์Šต๋‹ˆ๋‹ค!

๋˜ํ•œ, ๊ฐœ์„  ์ „ ์ฝ”๋“œ์—์„œ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ result์— ๋”ฐ๋ผ value๊ฐ’์ด ๋‹ฌ๋ผ์กŒ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌผ์Œํ‘œ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด String ๊ฐ’(intentValue)์„ ์ดˆ๊ธฐํ™”ํ•ด๋‘์–ด ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด if ~ else ๋ฌธ์„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์–ด์กŒ์Šต๋‹ˆ๋‹ค!

mode์˜ ์ข…๋ฅ˜๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ์ฝ”๋“œ์˜ ์–‘์€ ์ฒœ์ฐจ๋งŒ๋ณ„๋กœ ์ฐจ์ด๊ฐ€ ๋‚˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ ์ ˆํ•œ Collection ์‚ฌ์šฉ์€ ๋งŽ์€ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ผ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Comments