티스토리 뷰

======================================================

안녕하세요 Doridori 입니다. 


이번 강의는 HashTable과 Dictionary에 대한 강의 입니다. 


지난 강의에 진행했던 ArrayList와 List의 차이와 같은 차이점이 있습니다. 


따라서 지난강의와 같이(?) Dictionary에 대한 내용을 주로 다루게 되었습니다. ^^;;

======================================================


자료구조 3 (HashTable, Dictionary)


Source UI) Dictionary 예제 Image.zip

              (지난 강의 부터 시작 하였기 때문에 지난강의 전체 Source를 받아서 진행 하시면 될것 같습니다. 이미지는 별도 첨부 했습니다. )

Soruce 전체) Study_22_HashTable_Dictionary.zip

교재) 22강 자료구조 3 (HashTable, Dictionary).pdf






이번강의는 지난강의에서 이어지는 강의로 Dicitionary에 대한 내용을 다루고 있습니다. (HashTable도 잠시 언급은 했지만 사용 방법이 같음으로 차이점만 설명하고 넘어 가겠습니다.)


지난번과 비슷하게 List 구조를 가지는 Data이지만 Key, Value Type으로 구성되어 있습니다. 


프로그램 작성 시 배열 기준으로 List와 함께 가장 많이 사용되는 자료구조라고 보시면 될것 같습니다. 


List와의 가장 큰 차이는 Key 값을 별도로 등록해서 검색이 편하고 Key 값이 Unique 하기 때문에 값들이 명확해 진다는 장점이 있습니다. 


구조도 단순하고 사용하기 편하기 때문에 자주 사용하게 됩니다. 




UI의 경우 지난 강의에 진행 했던 "인기투표" 에서 개조 해서 사용 했습니다. 


구조가 비슷하기 때문에 비슷한 예제를 사용하는게 좋을것 같아서 . . . (예제 만들기가 힘들어서 ㅜㅠ . . . )


Dictionary의 특징 중 하나가 Key 값이 Unique 하다는 점이기 때문에 반 인원당 한번 씩 투표를 진행 하는 예제를 구성 해봤습니다.


(아래 TableLayoutPanel의 경우 Row를 하나 더 추가해서 4X3의 Table로 구성했습니다. (이미지가 잘못되어 있네요;;))





강의)


시작 할 때는 지난 강의에서 UI도 있겠다 조금만 수정해서 날로 먹어야지 라고 생각 했는데,


역시나 하다보니 40분짜리 강의가 되버렸렸습니다. ~(O.O)~


HashTable과 Dictionary의 차이, Enum의 사용 및 형 변환, KeyValuePair<string,string> Data 형 등을 신경쓰시면서 보시면 도움이 될것 같습니다. 


※ 강의에 나오는 반장 후보 친구들과, 반친구들의 이름이 익숙해보이는건 아마도 제가 아재이기는 하지만 젊은 아재(?) 라서 그럴꺼라고 생각합니다. ^^





Source)


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace _21_List
{
    public partial class Form1 : Form
    {
        enum enBossName
        {
            보검,
            신혜,
            해인,
            보영,
        }
 
        enum enClassName
        {
            진,
            정국,
            RM,
            지민,
            제이홉,
            뷔,
            슈가,
 
            은비,
            사쿠라,
            혜원,
            예나,
            채연,
            채원,
            민주,
            히토미,
            유리,
            유진,
            원영,
            나코,
        }
        
        List<string> _strList = new List<string>();  // List의 경우 Type을 선언 하고 사용
        ArrayList _arList = new ArrayList();
 
        int _iPlayerCount = 0;
 
        Hashtable _ht = new Hashtable();
        Dictionary<stringstring> _dic = new Dictionary<stringstring>();
 
        public Form1()
        {
            InitializeComponent();
 
 
            //dgViewList.Columns.Add("dgKey", "Key");  // Column 추가
            //dgViewList.Columns.Add("dgValue", "Value");  // Column 추가
        }
 
        private void pbox_Click(object sender, EventArgs e)
        {
            PictureBox pbox = sender as PictureBox;
 
            string strSelectText = string.Empty;
 
            switch (pbox.Name)
            {
                case "pbox1":
                    strSelectText = enBossName.보검.ToString();
                    break;
                case "pbox2":
                    strSelectText = enBossName.신혜.ToString();
                    break;
                case "pbox3":
                    strSelectText = enBossName.해인.ToString();
                    break;
                case "pbox4":
                    strSelectText = enBossName.보영.ToString();
                    break;
            }
 
            int iTotalCount = Enum.GetValues(typeof(enClassName)).Length;
 
            if (iTotalCount > _iPlayerCount)
            {
                enClassName enName = (enClassName)_iPlayerCount;
 
                _dic.Add(enName.ToString(), strSelectText);
 
                fUIDisplay(iTotalCount, enName.ToString());
                fDataGridViewDisplay();
 
                _iPlayerCount++;
            }
            else
            {
                lblPlayerName.Text = "투표를 완료 하였습니다.";
            }
 
        }
 
        bool _click = false;
 
        private void fUIDisplay(int iTotalCount, string strPlayerName)
        {
            int i보검 = 0;
            int i신혜 = 0;
            int i해인 = 0;
            int i보영 = 0;
 
            foreach (string oitem in _dic.Values)
            {
                // string 값을 enum으로 형변환 하는 부분
 
                switch (oitem)
                {
                    case "보검":
                        i보검++;
                        break;
                    case "신혜":
                        i신혜++;
                        break;
                    case "해인":
                        i해인++;
                        break;
                    case "보영":
                        i보영++;
                        break;
                }
            }
 
            lblPick1.Text = i보검.ToString();
            lblPick2.Text = i신혜.ToString();
            lblPick3.Text = i해인.ToString();
            lblPick4.Text = i보영.ToString();
 
 
            // 0 / 0
            lblTotalCount.Text = string.Format("{0} / {1}", _iPlayerCount +1, iTotalCount);
            lblPlayerName.Text = strPlayerName;
        }
 
 
        private void fDataGridViewDisplay()
        {
            //dgViewList.DataSource = _strList.Select(x => new { Value = x }).ToList();
 
            dgViewList.DataSource = _dic.ToArray();
 
 
            //dgViewList.Rows.Clear();
 
            //foreach (KeyValuePair<string,string> oitem in _dic)
            //{
            //    dgViewList.Rows.Add(oitem.Key, oitem.Value);
            //}
 
            foreach (DataGridViewRow oRow in dgViewList.Rows)
            {
                oRow.HeaderCell.Value = oRow.Index.ToString();
            }
 
            dgViewList.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
        }
 
    }
}
 
cs



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함