ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • System.Collections
    C# 2019. 6. 12. 20:58

    Collection이란 ?

     

    데이터를 구조적으로 모아둔 것. 대표적으로 ArrayList, Hashtable, Stack, Queue이 있다.

     

    ArrayList 필요에 따라 크기가 동적으로 증가하는 배열을 사용하여 IList 인터페이스를 구현합니다.
    Hashtable 키의 해시 코드에 따라 구성된 키/값 쌍의 컬렉션을 나타냅니다.
    Stack 제네릭이 아닌 간단한 LIFO(후입선출) 방식의 개체 컬렉션을 나타냅니다.
    Queue 개체의 선입선출(FIFO) 컬렉션을 나타냅니다.

     

    • ArrayList는 배열처럼 인덱스를 통해 바로 불러올 수 있다. 하지만 크기를 선언해야 하는 배열과 다르게 동적으로 크기가 증가한다.

     

    • Hashtable은 인덱스가 아닌 키(Key)를 이용해 값을 참조한다.

     

    ht["hello"]->hello

    ht["red"]->roja

    출처 : https://sites.google.com/site/usfcomputerscience/hash-tables-imp

     Stack은 FIFO Queue는 FILO

     

    using System;
    using System.Collections;

    namespace ConsoleApp133
    {
        class Program
        {
            static void Main()
            {
          
                ArrayList list = new ArrayList();
                Hashtable table = new Hashtable();
                Queue que = new Queue();
                Stack stack = new Stack();
               

                list.Clear();table.Clear();que.Clear();stack.Clear();

                for (int i = 0; i < 26; i++)
                {
                    list.Add((char)(i + 'A') + "");    //키가 0~25
                    table.Add(i,(char)(i + 'A') + "");
                    que.Enqueue((char)(i + 'A') + "");
                    stack.Push((char)(i + 'A') + "");

                }
                Print(list);Console.WriteLine("엘리먼트 개수 : {0}\n", list.Count);
                Print(que);Console.WriteLine("엘리먼트 개수 : {0}\n", que.Count);
                Print(stack);Console.WriteLine("엘리먼트 개수 : {0}\n", stack.Count);

                string msg1 = list[5] as string;
                Console.WriteLine("{0} 엘리먼트 개수 : {1}\n",msg1,list.Count);
                string msg4 = table[5] as string;
                Console.Write("{0} 엘리먼트 개수 : {1}\n", msg4, table.Count);
                string msg2 = que.Dequeue() as string;
                Console.Write("{0} 엘리먼트 개수 : {1}\n", msg2, que.Count);
                string msg3 = stack.Pop() as string;
                Console.Write("{0} 엘리먼트 개수 : {1}\n", msg3, stack.Count);
            }

            public static void Print(ICollection collect)
            {
                foreach (string str in collect)
                {
                    Console.WriteLine("{0}", str);
                }
                Console.WriteLine();
            }
        }
    }

     

    결과창

     

    (생략)

    ...

    F 엘리먼트 개수 : 26

    F 엘리먼트 개수 : 26
    A 엘리먼트 개수 : 25
    Z 엘리먼트 개수 : 25
    계속하려면 아무 키나 누르십시오 . . .

    'C#' 카테고리의 다른 글

    System.IO - FileStream  (0) 2019.06.11
    delegate  (0) 2019.06.10
    메모리의 특징 이해하기  (0) 2019.06.10
    C# polymorphism (2/4) - Override  (0) 2019.06.06
    Indexer  (0) 2019.06.06
Designed by Tistory.