본문 바로가기
카테고리 없음

[코딩일기] C# collection (List, Queue, Stack, HashTable, Dictionary, ArrayList)

by mania2321 2024. 6. 20.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;



public class Test : MonoBehaviour
{
    // ●●●●●●●●●●●●●●●●●●●● 변수 정의 및 대략적 설명 ●●●●●●●●●●●●●●●●●●●●

    int[] exp = new int[5] { 1, 2, 3, 4, 5 };       // 배열은 원소를 가감할 수 없다. 배열의 단점.
                                                    // 하지만 컬렉션은 원소를 가감할 수 있다.
                                                    // 컬렉션의 종류 : List, Queue, 스택, HashTable, Dictionary, ArrayList
    // ArrayList
    ArrayList arrayList = new ArrayList();          // ArrayList는 누군가가 만들어 놓은 클래스다.

    // List
    List<int/*명시한 자료형에 맞는 명령값을 넣어줘야 됨. arrayList와의 차이점*/> list = new List<int>();

    // HashTable
    Hashtable hashtable = new Hashtable();

    // Dictionary
    Dictionary<string, int> dictionary = new Dictionary<string, int>();     // HashTable과 유사함. ArrayList(HashTable)와 List(Dictionary)와의 관계와 매우 흡사.
                                                                            // 변수를 정의할 때 정하는 자료형(여기서는 string, int)을 철저히 따라야 됨. 안그러면 오류생김.
    // Queue                                        // 자료형을 명시해도 되고 명시하지 않아도 된다(여기서는 int라고 명시함). 선입 선출
    Queue<int> queue = new Queue<int>();            // ex) 유닛 생성 대기줄

    // Stack                                        // 자료형을 명시해도 되고 명시하지 않아도 된다(여기서는 int라고 명시함). 후입 선출(나중에 들어온 값이 먼저 나간다. 차곡차곡 쌓이는 개념)
    Stack<int> stack = new Stack<int>();            // 설거지 게임(설거지가 쌓이면 맨 위에 있는 설거지 거리부터 처리)

    private void Start()
    {
        // ●●●●●●●●●●●●●●●●●●●● ArrayList(예시 및 상세설명) ●●●●●●●●●●●●●●●●●●●●

        arrayList.Add(1);
        arrayList.Add(2);
        arrayList.Add(3);
        arrayList.Add("가나다라마바");
        arrayList.Add(4.5);

        // print(arrayList.Count);     // 배열에서는 .Length였지만, array에서는 .Count
        // 지우고 싶다면
        // arrayList.Remove("가나다라마바");    // 직접 삭제
        // arrayList.RemoveAt(3);               // 인덱스를 통해 삭제
        // arrayList.RemoveRange(1, 2);         // 인덱스 1 부터 2개 삭제하겠다
        // arrayList[3] = 4;                    // 가나다라마바 에서 4로 수정

        // arrayList.Clear                      // 이제까지의 arrayList를 싹 다 삭제
        // arrayList.Contains("가나다라마바")   // 있으면 true, 없으면 false 로 반환
        // arrayList.insert(1, 1.5);            // 인덱스 1번째 arrayList 다음에 1.5 생성

        for (int i = 0; i < arrayList.Count; i++)
        {
            print(arrayList[i]);
        }


        // ●●●●●●●●●●●●●●●●●●●● List(예시 및 상세설명) ●●●●●●●●●●●●●●●●●●●●

        list.Add(3);        // int로 명시했기 때문에 자료형 int형에 부합하는 값을 넣어줘야 된다.
                            // ArrayList는 마구잡이로 값을 넣어도 괜찮은 대신, List보다 더 많은 전산과정을 거쳐야 되기 때문에 List보다 확실히 과부화가 잘 걸리고 느리다.


        // ●●●●●●●●●●●●●●●●●●●● HashTable(예시 및 상세설명) ●●●●●●●●●●●●●●●●●●●●

        // hashtable.Add(키 값, 값);       =>      인덱스로 값을 찾는 것이 아니라, '키값'으로 값을 찾는다.

        hashtable.Add("만", 10000);
        hashtable.Add("백만", 1000000);
        hashtable.Add(50, "오십");

        print(hashtable["백만"]);
        print(hashtable[50]);


        // ●●●●●●●●●●●●●●●●●●●● Dictionary(예시 및 상세설명) ●●●●●●●●●●●●●●●●●●●●

        // Dictionary.Add(키 값, 값);       =>      인덱스로 값을 찾는 것이 아니라, '키값'으로 값을 찾는다.
        dictionary.Add("가", 100);


        // ●●●●●●●●●●●●●●●●●●●● Queue(예시 및 상세설명) ●●●●●●●●●●●●●●●●●●●●

        queue.Enqueue(5);       // 값 입력할 땐 .Enqueue, 값을 출력할 땐 .Dequeue
        queue.Enqueue(10);

        if(queue.Count != 0)            // 먼저 입력된 변수부터 차례대로 출력된다. 그냥 출력해도 되지만, 조건문 if를 갖다 대는 이유는, 입력된 값보다 출력명령이 더 많아지면 오류가 생기기 때문.
        {
            print(queue.Dequeue());
        }

        if(queue.Count != 0)
        {
            print(queue.Dequeue());
        }
        
        if(queue.Count != 0)
        {
            print(queue.Dequeue());
        }


        // ●●●●●●●●●●●●●●●●●●●● Stack(예시 및 상세설명) ●●●●●●●●●●●●●●●●●●●●

        stack.Push(1);       // 값 입력할 땐 '.Push', 값을 출력할 땐 '.Pop'
        stack.Push(2);
        stack.Push(3);

        if(stack.Count != 0)            // 먼저 입력된 변수부터 차례대로 출력된다. 그냥 출력해도 되지만, 조건문 if를 갖다 대는 이유는, 입력된 값보다 출력명령이 더 많아지면 오류가 생기기 때문.
        {
            print(stack.Pop());
        }
        
        if(stack.Count != 0)
        {
            print(stack.Pop());
        }
        
        if(stack.Count != 0)
        {
            print(stack.Pop());
        }

        if(stack.Count != 0)
        {
            print(stack.Pop());
        }
    }
}

댓글