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

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

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

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

25. ์ œ๋„ค๋ฆญ์Šค(Generics)  (0) 2022.04.07
24. yield ํ‚ค์›Œ๋“œ  (0) 2022.04.07
22. Queue, Stack, Hashtable ์‚ฌ์šฉ  (0) 2022.04.07
21. ArrayList ์‚ฌ์šฉํ•˜๊ธฐ  (0) 2022.04.07
20. ์ถ”์ƒํด๋ž˜์Šค์—์„œ ์ž๋™ ํ”„๋กœํผํ‹ฐ ์‚ฌ์šฉ๋ฒ•  (0) 2022.04.07
'๐Ÿ’ป Programming Language/C#' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • 25. ์ œ๋„ค๋ฆญ์Šค(Generics)
  • 24. yield ํ‚ค์›Œ๋“œ
  • 22. Queue, Stack, Hashtable ์‚ฌ์šฉ
  • 21. ArrayList ์‚ฌ์šฉํ•˜๊ธฐ
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
      ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ
      DP
      Java
      ์นด์นด์˜ค
      ์ž๋ฃŒ๊ตฌ์กฐ
      DART
      Flutter
      ์‚ผ์„ฑsw์—ญํ…Œ
      ์ฝ”๋“œํŠธ๋ฆฌ
      sort
      ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
      ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰
      ๊ตฌํ˜„
      ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
      ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
      JavaScript
      ์ด์ง„ํƒ์ƒ‰
      programmers
      Algorithm
      JS
      c#
      BFS
      ์•Œ๊ณ ๋ฆฌ์ฆ˜
    • ์ตœ๊ทผ ๋Œ“๊ธ€

    • ์ตœ๊ทผ ๊ธ€

    • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.1
    S.Honey
    23. ์ปฌ๋ ‰์…˜ ์ดˆ๊ธฐํ™”, ์ธ๋ฑ์„œ
    ์ƒ๋‹จ์œผ๋กœ

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