๐Ÿ’ป Programming Language/C#

27. ์ œ๋„ค๋ฆญ ์ปฌ๋ ‰์…˜

S.Honey 2022. 4. 7. 09:38

์ œ๋„ค๋ฆญ ์ปฌ๋ ‰์…˜(Generic Collection)์˜ ์กด์žฌ ๋ฐ ์‚ฌ์šฉ ์ด์œ 

  • Generic Collection : object ํ˜•์‹์— ๊ธฐ๋ฐ˜ํ•œ ๊ธฐ์กด ์ปฌ๋ ‰์…˜์˜ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค.
  • Generic(์ผ๋ฐ˜ํ™”) ๊ธฐ๋ฐ˜์œผ๋กœ ๋งŒ๋“ค์–ด์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ์‹œ ์‚ฌ์šฉํ•  ํ˜•์‹์ด ์ •ํ•ด์ง€๋ฏ€๋กœ ํ˜•๋ณ€ํ™˜ ๋ฐœ์ƒ์ด ์ค„์–ด๋“ ๋‹ค.
    • ์ปฌ๋ ‰์…˜์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์„ฑ๋Šฅ์ƒ์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

โญ๏ธโญ๏ธโญ๏ธ๊ธฐ์กด ์ปฌ๋ ‰์…˜์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์„ฑ๋Šฅ์ƒ์˜ ๋ฌธ์ œ
  • ์ปฌ๋ ‰์…˜์€ ๋ฐ์ดํ„ฐ๋ฅผ objectํ˜•์‹์œผ๋กœ ์ €์žฅํ•จ. int, double, string๊ณผ ๊ฐ™์€ ํƒ€์ž…์„ ์ปฌ๋ ‰์…˜์— ์ €์žฅํ•˜๊ฒŒ๋˜๋ฉด objectํ˜•์‹์œผ๋กœ ์ €์žฅํ•˜๊ณ  ์ปฌ๋ ‰์…˜์ด ์ปดํŒŒ์ผ๋ ๋•Œ ์ด์™€ ๊ฐ™์ด ํ˜•๋ณ€ํ™˜์ด ๋งค๋ฒˆ ์ผ์–ด๋‚˜๊ฒŒ๋จ. ๋˜ํ•œ ์ปดํŒŒ์ผ์‹œ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ปฌ๋ ‰์…˜ ๋‚ด ์š”์†Œ(๋ฐ์ดํ„ฐ)์— ์ ‘๊ทผํ•  ๋•Œ์—๋„ ํ˜•๋ณ€ํ™˜์ด ๊ณ„์†ํ•ด์„œ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ปฌ๋ ‰์…˜์˜ ์„ฑ๋Šฅ์— ๊ด€๋ จํ•œ ๋ฌธ์ œ๊ฐ€ ์žˆ์Œ
    • ์ด๋Š” Generic Collection์„ ํ†ตํ•ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ.

๊ธฐ์กด ์ปฌ๋ ‰์…˜๊ณผ ์ผ๋ฐ˜ํ™”(Generic) ๋ฒ„์ „

๊ธฐ์กด ์ผ๋ฐ˜ํ™” ๋ฒ„์ „
ArrayList List
Queue Queue
Stack Stack
Hashtable Dictionary<TKey, TValue>
1. List<T>๋Š” ArrayList์˜ ์ผ๋ฐ˜ํ™”(Generic)๋ฒ„์ „
2. Queue<T>๋Š” Queue์˜ ์ผ๋ฐ˜ํ™”(Generic)๋ฒ„์ „
3. Stack<T>๋Š” Stack์˜ ์ผ๋ฐ˜ํ™”(Generic)๋ฒ„์ „
4. Dictionary<TKey, TValue>๋Š” Hashtable์˜ ์ผ๋ฐ˜ํ™”(Generic)๋ฒ„์ „

์‚ฌ์šฉ ์ฝ”๋“œ ์˜ˆ์ œ


List


using System.Collections;

namespace GenericCollectionEx
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            for (int i = 0; i < 10; i++)
            {
                list.Add(i);
            }

            foreach (int i in list)
            {
                Console.Write($"{i} ");   
            }
            Console.WriteLine();

            list.RemoveAt(2);

            foreach (int i in list)
            {
                Console.Write($"{i} ");
            }
            Console.WriteLine();

            list.Insert(2, 2);

            foreach (int i in list)
            {
                Console.Write($"{i} ");
            }
            Console.WriteLine();
        }
    }
}
Output

0 1 2 3 4 5 6 7 8 9
0 1 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

Queue


using System.Collections;

namespace GenericCollectionEx
{   
    //Queue<T>
    class Program
    {
        static void Main(string[] args)
        {
            Queue<int> q = new Queue<int>();
            q.Enqueue(100);
            q.Enqueue(200);
            q.Enqueue(300);
            q.Enqueue(400);
            q.Enqueue(500);

            while(q.Count > 0)
            {
                Console.WriteLine(q.Dequeue()); 
            }
        }
    }
}
Output

100
200
300
400
500

Stack


using System.Collections;

namespace GenericCollectionEx
{   
    //Stack<T>
    class Program
    {
        static void Main(string[] args)
        {
            Stack<int> stack = new Stack<int>();
            stack.Push(11);
            stack.Push(22);
            stack.Push(33);
            stack.Push(44);
            stack.Push(55);

            while(stack.Count > 0)
            {
                Console.WriteLine(stack.Pop());
            }
        }
    }
}
Output

55
44
33
22
11

Dictionary<TKey, TValue>


using System.Collections;

namespace GenericCollectionEx
{   
    //Dictionary<TKey, TValue>
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic["apple"] = "์‚ฌ๊ณผ";
            dic["orange"] = "์˜ค๋ Œ์ง€";
            dic["banana"] = "๋ฐ”๋‚˜๋‚˜";
            dic["watermelon"] = "์ˆ˜๋ฐ•";

            Console.WriteLine(dic["apple"]);
            Console.WriteLine(dic["orange"]);
            Console.WriteLine(dic["watermelon"]);
            Console.WriteLine(dic["banana"]);
        }
    }
}
Output

์‚ฌ๊ณผ
์˜ค๋ Œ์ง€
์ˆ˜๋ฐ•
๋ฐ”๋‚˜๋‚˜

์ „์ฒด ์ฝ”๋“œ

using System.Collections;

class Program
{
    static void Main(string[] args)
    {
        List<int> list = new List<int>();
        for (int i = 0; i < 10; i++)
        {
            list.Add(i);
        }

        Console.WriteLine("-----List-----");
        foreach (int i in list)
        {
            Console.Write($"{i} ");
        }

        Console.WriteLine("\n\n-----Queue-----");
        Queue<int> q = new Queue<int>();
        q.Enqueue(100);
        q.Enqueue(200);
        q.Enqueue(300);
        q.Enqueue(400);
        q.Enqueue(500);

        while (q.Count > 0)
        {
            Console.Write(q.Dequeue()+" ");
        }
        Console.WriteLine("\n\n-----Stack-----");
        Stack<int> stack = new Stack<int>();
        stack.Push(11);
        stack.Push(22);
        stack.Push(33);
        stack.Push(44);
        stack.Push(55);

        while (stack.Count > 0)
        {
            Console.Write(stack.Pop() + " ");
        }
        Console.WriteLine("\n\n-----Dictionary-----");
        Dictionary<string, string> dic = new Dictionary<string, string>();
        dic["apple"] = "์‚ฌ๊ณผ";
        dic["orange"] = "์˜ค๋ Œ์ง€";
        dic["banana"] = "๋ฐ”๋‚˜๋‚˜";
        dic["watermelon"] = "์ˆ˜๋ฐ•";

        Console.Write(dic["apple"] + " ");
        Console.Write(dic["orange"] + " ");
        Console.Write(dic["watermelon"] + " ");
        Console.WriteLine(dic["banana"]);
    }
}
Output

-----List-----
0 1 2 3 4 5 6 7 8 9

-----Queue-----
100 200 300 400 500

-----Stack-----
55 44 33 22 11

-----Dictionary-----
์‚ฌ๊ณผ ์˜ค๋ Œ์ง€ ์ˆ˜๋ฐ• ๋ฐ”๋‚˜๋‚˜