딕셔너리

jinsuk·2023년 9월 9일
using System;
using System.Collections.Generic;

namespace csharp
{
    class Program
    {
        class Monster
        {
            public int id;
            public Monster(int id) {this.id = id;}
        }
        static void Main(string[] args)
        {
            List<int> list = new List<int>();

            // 만약 리스트에 몬스터 100만 마리가 있고 특정 몬스터를 찾아야 한다면? 
            // [~~~~~~~~] 처음부터 끝까지 다 찾아보는 방법밖에 없다.

            // Key -> Value
            // Dictionary -> 키값을 안다면 벨류값을 빠르게 찾을 수 있다! 반대는 적용 x
            // Hashtable 기법 사용
            // -> 메모리를 많이 사용하는 대신, 성능을 취한다.
            // -> 아주 큰 박스가 아닌 여러개의 작은박스에 공 1번부터 10000번까지를 나누어 담는다.

            Dictionary<int, Monster> dic = new Dictionary<int, Monster>();
            for (int i = 0; i < 10000; i++)
            {
                dic.Add(i, new Monster(i));
            }

            Monster mon;
            bool found = dic.TryGetValue(7777, out mon);

            dic.Remove(7777);
            dic.Clear();
        }
    }
}
profile
공부기록용

0개의 댓글