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

2022. 4. 7. 09:38ยท๐Ÿ’ป Programming Language/C#

์ œ๋„ค๋ฆญ ์ปฌ๋ ‰์…˜(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-----
์‚ฌ๊ณผ ์˜ค๋ Œ์ง€ ์ˆ˜๋ฐ• ๋ฐ”๋‚˜๋‚˜

'๐Ÿ’ป Programming Language > C#' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

29-1. Null ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž(Null Operator)  (0) 2022.04.07
28. ์˜ˆ์™ธ์ฒ˜๋ฆฌ(Exception Handling)  (0) 2022.04.07
26. ์ œ๋„ค๋ฆญ์Šค(Generics) ํƒ€์ž… ์ œ์•ฝ  (0) 2022.04.07
25. ์ œ๋„ค๋ฆญ์Šค(Generics)  (0) 2022.04.07
24. yield ํ‚ค์›Œ๋“œ  (0) 2022.04.07
'๐Ÿ’ป Programming Language/C#' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • 29-1. Null ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž(Null Operator)
  • 28. ์˜ˆ์™ธ์ฒ˜๋ฆฌ(Exception Handling)
  • 26. ์ œ๋„ค๋ฆญ์Šค(Generics) ํƒ€์ž… ์ œ์•ฝ
  • 25. ์ œ๋„ค๋ฆญ์Šค(Generics)
S.Honey
S.Honey
  • S.Honey
    Algo ์“ฐ์ž
    S.Honey
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (123)
      • ํšŒ๊ณ  (0)
        • ์ทจ์—… ํ›„ ํšŒ๊ณ  (0)
      • ๐Ÿƒ Frontend Road-Map (2)
        • ๐Ÿšฉ Summary (1)
        • ๐Ÿ“š Road-Map Contents (1)
        • ๐ŸŸง HTML (0)
        • ๐ŸŸฆ CSS (0)
        • ๐ŸŸจ Javascript (0)
        • โฌœ React (0)
        • ๐ŸŸช Redux (0)
      • Backend (0)
        • QueryDSL (0)
      • ๐Ÿ’ป Programming Language (54)
        • C# (51)
        • Flutter-Dart (3)
        • Java (0)
      • ๐Ÿ“š Computer Science (4)
        • Algorithms (4)
        • Database (0)
        • Network (0)
        • Operating System(OS) (0)
      • ๐Ÿ’ฏ CodingTest (60)
        • BaekJoon (22)
        • Programmers (34)
        • CodeTree (4)
      • โœ’๏ธ Design Pattern (1)
      • ๐Ÿฑ Etc (2)
        • Jenkins Plugin ์ œ์ž‘๊ธฐ (1)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ๋งํฌ

    • ๊ณต์ง€์‚ฌํ•ญ

      • ๐Ÿ“– ๊ณต๋ถ€ ์ฐธ๊ณ  ๊ต์žฌ ๋ฐ ์ž๋ฃŒ
    • ์ธ๊ธฐ ๊ธ€

    • ํƒœ๊ทธ

      ํŒŒ์ด์ฌ
      BAEKJOON
      ์•Œ๊ณ ๋ฆฌ์ฆ˜
      ์ฝ”๋“œํŠธ๋ฆฌ
      ์“ฐ์…จ์ž–์•„
      Algorithm
      JavaScript
      ์ด์ง„ํƒ์ƒ‰
      ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ
      ๊ตฌํ˜„
      JS
      ๋ฐฑ์ค€
      Flutter
      ์นด์นด์˜ค
      Java
      ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
      c#
      ๋ฌธ์ž์—ด ํŒŒ์‹ฑ
      ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
      ์ž๋ฃŒ๊ตฌ์กฐ
      ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰
      sort
      DP
      ์‹œ๋ฎฌ๋ ˆ์ด์…˜
      programmers
      ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
      BFS
      ์‚ผ์„ฑsw์—ญํ…Œ
      ์Šคํ„ฐ๋””
      DART
    • ์ตœ๊ทผ ๋Œ“๊ธ€

    • ์ตœ๊ทผ ๊ธ€

    • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.1
    S.Honey
    27. ์ œ๋„ค๋ฆญ ์ปฌ๋ ‰์…˜
    ์ƒ๋‹จ์œผ๋กœ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”