■ IList 인터페이스를 사용하는 방법을 보여준다.
▶ 예제 코드 (C#)
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 |
using System; SimpleList simpleList = new SimpleList(); Console.WriteLine("리스트 만들기"); simpleList.Add("one" ); simpleList.Add("two" ); simpleList.Add("three"); simpleList.Add("four" ); simpleList.Add("five" ); simpleList.Add("six" ); simpleList.Add("seven"); simpleList.Add("eight"); Console.WriteLine(); simpleList.DisplayStatus(); Console.WriteLine(); Console.WriteLine("[리스트에서 요소 제거하기]"); simpleList.Remove("six"); simpleList.Remove("eight"); Console.WriteLine(); simpleList.DisplayStatus(); Console.WriteLine(); Console.WriteLine("[리스트 끝에 요소 추가하기]"); simpleList.Add("nine"); Console.WriteLine(); simpleList.DisplayStatus(); Console.WriteLine(); Console.WriteLine("[리스트 중간에 요소 삽입하기]"); simpleList.Insert(4, "number"); Console.WriteLine(); simpleList.DisplayStatus(); Console.WriteLine(); Console.WriteLine("[리스트에서 특정 요소 체크하기]"); Console.WriteLine(); Console.WriteLine("리스트가 \"three\"를 포함한다 : {0}", simpleList.Contains("three")); Console.WriteLine("리스트가 \"ten\"을 포함한다 : {0}", simpleList.Contains("ten" )); |
▶ SimpleList.cs
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
using System; using System.Collections; /// <summary> /// 심플 리스트 /// </summary> public class SimpleList : IList { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 객체 배열 /// </summary> private object[] objectArray = new object[8]; /// <summary> /// 카운트 /// </summary> private int count; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public // IList #region 리스트 고정 크기 여부 - IsFixedSize /// <summary> /// 리스트 고정 크기 여부 /// </summary> public bool IsFixedSize { get { return true; } } #endregion // IList #region 읽기 전용 여부 - IsReadOnly /// <summary> /// 읽기 전용 여부 /// </summary> public bool IsReadOnly { get { return false; } } #endregion // IList #region 인덱서 - this[index] /// <summary> /// 인덱서 /// </summary> /// <param name="index">인덱스</param> /// <returns>항목</returns> public object this[int index] { get { return this.objectArray[index]; } set { this.objectArray[index] = value; } } #endregion // ICollection #region 카운트 - Count /// <summary> /// 카운트 /// </summary> public int Count { get { return this.count; } } #endregion // ICollection #region 동기화 여부 - IsSynchronized /// <summary> /// 동기화 여부 /// </summary> public bool IsSynchronized { get { return false; } } #endregion // ICollection #region 동기화 객체 - SyncRoot /// <summary> /// 동기화 객체 /// </summary> public object SyncRoot { get { return this; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SimpleList() /// <summary> /// 생성자 /// </summary> public SimpleList() { this.count = 0; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public // IList #region 추가하기 - Add(value) /// <summary> /// 추가하기 /// </summary> /// <param name="value">값</param> /// <returns>인덱스</returns> public int Add(object value) { if(this.count < this.objectArray.Length) { this.objectArray[this.count] = value; this.count++; return (this.count - 1); } else { return -1; } } #endregion // IList #region 지우기 - Clear() /// <summary> /// 지우기 /// </summary> public void Clear() { this.count = 0; } #endregion // IList #region 포함 여부 구하기 - Contains(value) /// <summary> /// 포함 여부 구하기 /// </summary> /// <param name="value">값</param> /// <returns>포함 여부</returns> public bool Contains(object value) { bool inList = false; for(int i = 0; i < Count; i++) { if(this.objectArray[i] == value) { inList = true; break; } } return inList; } #endregion // IList #region 인덱스 구하기 - IndexOf(value) /// <summary> /// 인덱스 구하기 /// </summary> /// <param name="value">값</param> /// <returns></returns> public int IndexOf(object value) { int index = -1; for(int i = 0; i < Count; i++) { if(this.objectArray[i] == value) { index = i; break; } } return index; } #endregion // IList #region 삽입하기 - Insert(index, value) /// <summary> /// 삽입하기 /// </summary> /// <param name="index">인덱스</param> /// <param name="value">값</param> public void Insert(int index, object value) { if((this.count + 1 <= this.objectArray.Length) && (index < Count) && (index >= 0)) { this.count++; for(int i = Count - 1; i > index; i--) { this.objectArray[i] = this.objectArray[i - 1]; } this.objectArray[index] = value; } } #endregion // IList #region 제거하기 - Remove(value) /// <summary> /// 제거하기 /// </summary> /// <param name="value">값</param> public void Remove(object value) { RemoveAt(IndexOf(value)); } #endregion // IList #region 제거하기 - RemoveAt(index) /// <summary> /// 제거하기 /// </summary> /// <param name="index">인덱스</param> public void RemoveAt(int index) { if((index >= 0) && (index < Count)) { for(int i = index; i < Count - 1; i++) { this.objectArray[i] = this.objectArray[i + 1]; } this.count--; } } #endregion // ICollection #region 복사하기 - CopyTo(array, index) /// <summary> /// 복사하기 /// </summary> /// <param name="array">배열</param> /// <param name="index">인덱스</param> public void CopyTo(Array array, int index) { int j = index; for(int i = 0; i < Count; i++) { array.SetValue(this.objectArray[i], j); j++; } } #endregion // IEnumerable #region 열거자 구하기 - GetEnumerator() /// <summary> /// 열거자 구하기 /// </summary> /// <returns>열거자</returns> public IEnumerator GetEnumerator() { throw new Exception("The method or operation is not implemented."); } #endregion #region 상태 출력하기 - DisplayStatus() /// <summary> /// 상태 출력하기 /// </summary> public void DisplayStatus() { Console.WriteLine("리스트는 {0}개 용량과 현재 {1}개 요소를 갖는다.", this.objectArray.Length, this.count); Console.Write("리스트 : "); for(int i = 0; i < Count; i++) { Console.Write(" {0}", this.objectArray[i]); } Console.WriteLine(); } #endregion } |