05.Dictionary — 웹툰PD 지망생 : 정민재
05.Dictionary
●/섹션 6. 자료구조 맛보기

05.Dictionary

728x90

 

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        // 돌아다니는 모든 생명체에 ID 부여
        // 103번 ID 몬스터를 공격하고 싶다
        // 103번 ID를 넣고 -> 공격이라는 네트워크 요청에 해당하는 아이디를 넣는다
        // ex) 10 103 id

        //List<int> list = new List<int>();
        //여기서 동적배열(List)의 문제점은 어떻게 id를 찾느냐에 문제가 된다.
        //100만마리(1,000,000 마리) 의 몬스터들 중에서 103번 몬스터를 어떻게 찾아야 하나??
        //하나하나씩 체크해야 하나?

        //해결책 : Key -> Value
        //Dictionary 출현

        class Monster
        {            
            public int id;

            public Monster(int _id)
            {
                this.id = _id;
            }
        }

        static void Main(string[] args)
        {
            // Dictionary< Key,Value > -----> Key를 알면 Value를 굉장히 빠르게 찾을 수 있다.
            Dictionary<int, Monster> dic = new Dictionary<int, Monster>();

            //넣기1
            dic.Add(1, new Monster(1));

            //넣기2
            dic[5] = new Monster(5);
        }
    }
}

 

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        // 돌아다니는 모든 생명체에 ID 부여
        // 103번 ID 몬스터를 공격하고 싶다
        // 103번 ID를 넣고 -> 공격이라는 네트워크 요청에 해당하는 아이디를 넣는다
        // ex) 10 103 id

        //List<int> list = new List<int>();
        //여기서 동적배열(List)의 문제점은 어떻게 id를 찾느냐에 문제가 된다.
        //100만마리(1,000,000 마리) 의 몬스터들 중에서 103번 몬스터를 어떻게 찾아야 하나??
        //하나하나씩 체크해야 하나?

        //해결책 : Key -> Value
        //Dictionary 출현

        class Monster
        {            
            public int id;

            public Monster(int _id)
            {
                this.id = _id;
            }
        }

        static void Main(string[] args)
        {
            // Dictionary< Key,Value > -----> Key를 알면 Value를 굉장히 빠르게 찾을 수 있다.


            Dictionary<int, Monster> dic = new Dictionary<int, Monster>();

            //넣기
            for(int i=0; i<10000; i++)
            {
                dic.Add(i, new Monster(i));
            }

            //번호 5000번 몬스터를 가져오기
            Monster mon = dic[5000];
        }
    }
}

 

프로그램이 뻗는 경우

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        // 돌아다니는 모든 생명체에 ID 부여
        // 103번 ID 몬스터를 공격하고 싶다
        // 103번 ID를 넣고 -> 공격이라는 네트워크 요청에 해당하는 아이디를 넣는다
        // ex) 10 103 id

        //List<int> list = new List<int>();
        //여기서 동적배열(List)의 문제점은 어떻게 id를 찾느냐에 문제가 된다.
        //100만마리(1,000,000 마리) 의 몬스터들 중에서 103번 몬스터를 어떻게 찾아야 하나??
        //하나하나씩 체크해야 하나?

        //해결책 : Key -> Value
        //Dictionary 출현

        class Monster
        {            
            public int id;

            public Monster(int _id)
            {
                this.id = _id;
            }
        }

        static void Main(string[] args)
        {
            // Dictionary< Key,Value > -----> Key를 알면 Value를 굉장히 빠르게 찾을 수 있다.


            Dictionary<int, Monster> dic = new Dictionary<int, Monster>();

            //넣기
            for(int i=0; i<10000; i++)
            {
                dic.Add(i, new Monster(i));
            }

            //번호 20000번 몬스터를 가져오기
            Monster mon = dic[20000];
        }
    }
}

 

몬스터를 10000까지만 저장했는데

 

20000번째 몬스터를 부르면 프로그램이 뻗는다.

 

그래서 이렇게 접근하는 건 위험하다.

 

 

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        // 돌아다니는 모든 생명체에 ID 부여
        // 103번 ID 몬스터를 공격하고 싶다
        // 103번 ID를 넣고 -> 공격이라는 네트워크 요청에 해당하는 아이디를 넣는다
        // ex) 10 103 id

        //List<int> list = new List<int>();
        //여기서 동적배열(List)의 문제점은 어떻게 id를 찾느냐에 문제가 된다.
        //100만마리(1,000,000 마리) 의 몬스터들 중에서 103번 몬스터를 어떻게 찾아야 하나??
        //하나하나씩 체크해야 하나?

        //해결책 : Key -> Value
        //Dictionary 출현

        class Monster
        {            
            public int id;

            public Monster(int _id)
            {
                this.id = _id;
            }
        }

        static void Main(string[] args)
        {
            // Dictionary< Key,Value > -----> Key를 알면 Value를 굉장히 빠르게 찾을 수 있다.


            Dictionary<int, Monster> dic = new Dictionary<int, Monster>();

            //넣기
            for(int i=0; i<10000; i++)
            {
                dic.Add(i, new Monster(i));
            }

            //번호 20000번 몬스터를 가져오기
            Monster mon;
            bool found = dic.TryGetValue(20000, out mon);
        }
    }
}

오류가 생기지 않는다

 

 

 

 

Dictionary <- HashTable 기법

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        // 돌아다니는 모든 생명체에 ID 부여
        // 103번 ID 몬스터를 공격하고 싶다
        // 103번 ID를 넣고 -> 공격이라는 네트워크 요청에 해당하는 아이디를 넣는다
        // ex) 10 103 id

        //List<int> list = new List<int>();
        //여기서 동적배열(List)의 문제점은 어떻게 id를 찾느냐에 문제가 된다.
        //100만마리(1,000,000 마리) 의 몬스터들 중에서 103번 몬스터를 어떻게 찾아야 하나??
        //하나하나씩 체크해야 하나?

        //해결책 : Key -> Value
        //Dictionary 출현

        class Monster
        {            
            public int id;

            public Monster(int _id)
            {
                this.id = _id;
            }
        }

        static void Main(string[] args)
        {
            // Dictionary< Key,Value > -----> Key를 알면 Value를 굉장히 빠르게 찾을 수 있다.


            Dictionary<int, Monster> dic = new Dictionary<int, Monster>();

            //넣기
            for(int i=0; i<10000; i++)
            {
                dic.Add(i, new Monster(i));
            }

            //번호 7777번 몬스터를 가져오기
            Monster mon;
            bool found = dic.TryGetValue(7777, out mon);

            //삭제
            dic.Remove(7777);


            //전체 삭제
            dic.Clear();


            // Dictionary = HashTable 기법
            // ex : 아주 큰 박스안에 공이 들어있음 [ ㅇ  ㅇ  ㅇ ㅇ ㅇ  ㅇ] 1만개(1 ~ 10000)
            // 박스 여러개를 쪼개 놓는다.  [   ] [ ] [ ] [  ]  [ ] [ ]   [ ]  [ ]  [ ] [ ] [ ] 박스 1천개
            // 그러면 공을 10개씩 1박스에 집어 넣는 꼴 -> [1~10] [11~20] [  ] [  ] .....
            // 7777번 공? -> 777번 상자에 있겠네

            // HashTable 단점 : 박스를 굉~~장히 많이 준비해야 한다  ->  메모리 큰 손해
            // 즉 HashTable 한 줄 요약 : [ 메모리를 내주고, 성능을 취한다 ! ] 

        }
    }
}
728x90

' > 섹션 6. 자료구조 맛보기' 카테고리의 다른 글

04.List  (0) 2021.07.13
02.연습문제  (0) 2021.07.13
01.배열  (0) 2021.07.13