C#/์ด๊ฒƒ์ด C#์ด๋‹ค

58. ์ผ๋ฐ˜ํ™” ์ปฌ๋ ‰์…˜

Rainbow๐ŸŒˆCoder 2022. 2. 28. 17:22
728x90

์ผ๋ฐ˜ํ™” ์ปฌ๋ ‰์…˜์€ object ํ˜•์‹ ๊ธฐ๋ฐ˜์˜ ์ปฌ๋ ‰์…˜์ด ๊ฐ–๊ณ  ์žˆ๋˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.

์ผ๋ฐ˜ํ™” ์ปฌ๋ ‰์…˜์€ ์ผ๋ฐ˜ํ™”์— ๊ธฐ๋ฐ˜ํ•ด์„œ ๋งŒ๋“ค์–ด์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ํ•  ๋•Œ ์ปฌ๋ ‰์…˜์—์„œ ์‚ฌ์šฉํ•  ํ˜•์‹์ด ๊ฒฐ์ •๋˜๊ณ ,

์“ธ๋ฐ์—†๋Š” ํ˜•์‹ ๋ณ€ํ™˜์„ ์ผ์œผํ‚ค์ง€ ์•Š๋Š”๋‹ค. ๋˜ํ•œ ์ž˜๋ชป๋œ ํ˜•์‹์˜ ๊ฐ์ฒด๋ฅผ ๋‹ด๊ฒŒ ๋  ์œ„ํ—˜๋„ ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค.

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

 

object ์ปฌ๋ ‰์…˜ ์ผ๋ฐ˜ํ™” ์ปฌ๋ ‰์…˜
ArrayList List<T>
Queue Queue<T>
Stack Stack<T>
Hashtable Dictionary<TKey,TValue>

List<T>, Queue<T>, Stack<T>, Dictionary<TKey, TValue>๋Š” ๊ฐ๊ฐ 

ArrayLisst, Queue, Stack, Hashtable์˜ ์ผ๋ฐ˜ํ™” ๋ฒ„์ „์ด๋‹ค.

์“ฐ๊ธฐ ์œ„ํ•ด์„œ

ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

using System.Collections.Generic;

 

<List<T>์˜ˆ์ œ>

using System;
using System.Collections.Generic;

namespace UsingGenericList
{
    class MainApp
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            for (int i = 0; i < 5; i++) list.Add(i);

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

            list.RemoveAt(2);

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

            list.Insert(2, 2);

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

        }
    }
}

<์ถœ๋ ฅ ๊ฒฐ๊ณผ>

01234
0134
0 1 2 3 4

<Queue<T>์˜ˆ์ œ>

using System;
using System.Collections.Generic;

namespace UsingGenericQueue
{
    class MainApp
    {
        static void Main(string[] args)
        {
            Queue<int> queue = new Queue<int>();
            for (int i = 0; i < 5; i++) queue.Enqueue(i);

            while (queue.Count>0) Console.WriteLine(queue.Dequeue());
        }
    }
}

<์ถœ๋ ฅ ๊ฒฐ๊ณผ>

0
1
2
3
4

<Stack<T>์˜ˆ์ œ>

using System;
using System.Collections.Generic;

namespace UsingGenericStack
{
    class MainApp
    {
        static void Main(string[] args)
        {
            Stack<int> stack = new Stack<int>();
            for (int i = 0; i < 5; i++) stack.Push(i);

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

<์ถœ๋ ฅ ๊ฒฐ๊ณผ>

4
3
2
1
0

<Dictionary<TKey, Tvalue> ์˜ˆ์ œ>

using System;
using System.Collections.Generic;

namespace UsingGenericDictionary
{
    class MainApp
    {
        static void Main(string[] args)
        {
            Dictionary<string,string> dictionary = new Dictionary<string,string>();
            dictionary["ํ•˜๋‚˜"] = "one";
            dictionary["๋‘˜"] = "two";
            dictionary["์…‹"] = "three";
            dictionary["๋„ท"] = "four";
            dictionary["๋‹ค์„ฏ"] = "five";

            Console.WriteLine(dictionary["ํ•˜๋‚˜"]);
            Console.WriteLine(dictionary["๋‘˜"]);
            Console.WriteLine(dictionary["์…‹"]);
            Console.WriteLine(dictionary["๋„ท"]);
            Console.WriteLine(dictionary["๋‹ค์„ฏ"]);

        }
    }
}

<์ถœ๋ ฅ ๊ฒฐ๊ณผ>

one
two
three
four
five
728x90