๐Ÿ’ป Programming Language/C#

23. ์ปฌ๋ ‰์…˜ ์ดˆ๊ธฐํ™”, ์ธ๋ฑ์„œ

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

ArrayList, Stack, Queue


using System.Collections;
using static System.Console;
//์ด์™€ ๊ฐ™์ด using static System.Console; ์„ ์„ ์–ธํ•˜๋ฉด ๋ณธ๋ฌธ์—์„œ Console์„ ์ œ์™ธํ•˜๊ณ 
// WriteLine()๋งŒ์œผ๋กœ ์ถœ๋ ฅ๋ฌธ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.


namespace CollectionInitialEx
{
    class Program
    {
        static void Main(string[] args)
        {
            //์ปฌ๋ ‰์…˜ ์ดˆ๊ธฐ์ž๋ฅผ ์ด์šฉํ•œ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ• => Stack, Queue์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
            //Stack์ด๋‚˜ Queue๋Š” Add๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ๋•Œ๋ฌธ
            //์ปฌ๋ ‰์…˜ ์ดˆ๊ธฐ์ž๋Š” IEnumerable์ด๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ Add()๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋‹ค.
            ArrayList al = new ArrayList() {10, 20, 30};

            int[] array = { 11, 22, 33, 44, 55 };
            ArrayList al2 = new ArrayList(array);
            foreach (Object o in al2)
            {
                Console.WriteLine($"ArrayList : {o}");
            }
            WriteLine();

            Stack stack = new Stack(array);
            foreach (Object o in stack)
            {
                Console.WriteLine($"Stack : {o}");
            }
            WriteLine();

            Queue q = new Queue(array);
            foreach(Object o in q)
            {
                WriteLine($"Queue : {o}");
            }
        }
    }
}
Output

ArrayList : 11
ArrayList : 22
ArrayList : 33
ArrayList : 44
ArrayList : 55

Stack : 55
Stack : 44
Stack : 33
Stack : 22
Stack : 11

Queue : 11
Queue : 22
Queue : 33
Queue : 44
Queue : 55
  • ์ปฌ๋ ‰์…˜ ์ดˆ๊ธฐ์ž๋ฅผ ์ด์šฉํ•œ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ• => Stack, Queue์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
  • Stack์ด๋‚˜ Queue๋Š” Add๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ๋•Œ๋ฌธ
  • ์ปฌ๋ ‰์…˜ ์ดˆ๊ธฐ์ž๋Š” IEnumerable์ด๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ Add()๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋‹ค.

using static System.Console;
  • ์ด์™€ ๊ฐ™์ด using static System.Console; ์„ ์„ ์–ธํ•˜๋ฉด ๋ณธ๋ฌธ์—์„œ Console์„ ์ œ์™ธํ•˜๊ณ  WriteLine()๋งŒ์œผ๋กœ ์ถœ๋ ฅ๋ฌธ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

Hashtable

using System.Collections;

namespace HashtableInitialEx
{
    //Hashtable ์ดˆ๊ธฐํ™” ํ•  ๋•Œ ๋”•์…”๋„ˆ๋ฆฌ ์ดˆ๊ธฐ์ž(Dictionary Initialiser)๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable()
            {
                ["seoul"] = "์„œ์šธ",
                ["incheon"] = "์ธ์ฒœ",
                ["kwangju"] = "๊ด‘์ฃผ"
            };

            Console.WriteLine(ht["seoul"]);
            Console.WriteLine(ht["incheon"]);
            Console.WriteLine(ht["kwangju"]);


            Hashtable ht2 = new Hashtable()
            {
                { "seoul", "์„œ์šธ" },
                { "incheon", "์ธ์ฒœ" },
                { "kwangju", "๊ด‘์ฃผ" },
            };

            Console.WriteLine(ht2["seoul"]);
            Console.WriteLine(ht2["incheon"]);
            Console.WriteLine(ht2["kwangju"]);
        }
    }
}
Output

์„œ์šธ
์ธ์ฒœ
๊ด‘์ฃผ
์„œ์šธ
์ธ์ฒœ
๊ด‘์ฃผ
  • Hashtable์„ ์ดˆ๊ธฐํ™” ํ•  ๋•Œ๋”•์…”๋„ˆ๋ฆฌ ์ดˆ๊ธฐ์ž(Dictionary Initialiser)๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์œ„์˜ ์ฝ”๋“œ์—์„œ์ฒ˜๋Ÿผ ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , ํ›„์ž์˜ ๋ฐฉ์‹์€ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ๋„ ์‚ฌ์šฉ๋œ๋‹ค.

Indexer(์ธ๋ฑ์„œ)


  • ์ธ๋ฑ์„œ(Indexer) : ํด๋ž˜์Šค ๊ฐ์ฒด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์•ก์„ธ์Šค ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๊ฒƒ.
    • ๊ฐ์ฒด๋ฅผ ๋งˆ์น˜ ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.

์ธ๋ฑ์Šค ์„ ์–ธ ๋ฐฉ๋ฒ•

        class ํด๋ž˜์Šค๋ช… 
        {
            ์ ‘๊ทผ์ œํ•œ์ž ์ธ๋ฑ์„œํ˜•์‹ this[ํ˜•์‹ index]
            {
                get
                {
                    idx๋ฅผ ์ด์šฉํ•ด์„œ ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜
                }
                set
                {
                    idx๋ฅผ ์ด์šฉํ•ด์„œ ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ    
                }
            }
        }

namespace IndexerEx
{

    class DemoList
    {
        private int[] array;

        public DemoList()
        {
            array = new int[4];
        }

        // ์ธ๋ฑ์„œ ์„ ์–ธ๋ถ€
        public int this[int idx]
        {

            get { return array[idx]; }

            set
            {
                if (idx >= array.Length)
                {
                    Array.Resize<int>(ref array, idx + 1);
                    Console.WriteLine($"๋ฐฐ์—ด์‚ฌ์ด์ฆˆ ์กฐ์ • : {array.Length}");
                }
                array[idx] = value;
            }
        }

        public int Length
        {
            get { return array.Length; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            DemoList list = new DemoList();

            for (int i = 0; i < 5; i++)
            {
                list[i] = i;
            }

            for (int i = 0; i < list.Length; i++)
            {
                Console.WriteLine(list[i]); // ์ธ๋ฑ์„œ๋ฅผ ํ†ตํ•ด ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
            }
        }
    }
}
Output

๋ฐฐ์—ด์‚ฌ์ด์ฆˆ ์กฐ์ • : 5
0
1
2
3
4