유니티로 배우는 c#

컬렉션

테오구 2021. 10. 3. 17:17
728x90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    // 배열
    int[] exp = new int[5]{1,2,3,4,5};
    
    // ArrayList
    // 컬렉션 : 리스트, 큐, 스택, 해시테이블, 딕셔너리, 어레이리스트

    ArrayList arrayList = new ArrayList();
    //누군가 만들어 놓은 ArrayList를 사용하기 위해 using System.Collections; 선언
    // list보다 더 연산 양이 많아 좋지 않다.
    // 어떤 자료형에도 얼메이지 않는 것을 쓰고 싶을 때 사용

    // List

    List<int> list = new List<int>();
    
    // HashTable
    HashTable hashTable = new HashTable();

    // Dictionary
    Dictionary<string, int> dictionary = new Dictionary<string, int>();
    // HashTable보다 연산이 더 빠르다

    // Queue 선입선출, 자료형을 명시해도 좋고 안해도 좋다. 표션 제작 대기줄
    Queue<int> queue = new Queue<int>();

    // Stack 후입선출 
    Stack<int> stack = new Stack<int>();


    void Start(){
        arrayList.Add(1);
        arrayList.Add(2);
        arrayList.Add(3);
        arrayList.Add("가나다라");
        arrayList.Add(4); //특정한 값을 더해준다.

        array.Remove(4); //array인덱스를 통해 지워 준다.
        array.RemoveRange(1,3) //인덱스1번부터 3개를 지웁니다.
        array[3] = 4//세번째 인덱스의 값을 4로 바꾼다.

        print(arrayList.Count); //어레이 리스트에는 length가 없다. 대신 count를 사용

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

        list.Add(3)//타입을 정해 주었기 때문에 그것만 사용 가능하다.

        hashTable.Add("만", 10000);
        hashTable.Add("백만", 1000000);
        hashTable.Add(50, "1억");

        print(hashTable["만"]); //해시테이블은 키값으로 접근해야한다.
        print(hashTable[50]); //1억이 프린트 됨

        dictionary.Add(3);

        queue.Enqueue(5);
        queue.Enqueue(15);
        print(queue.Dequeue());
        print(queue.Dequeue());

        stack.Push(1);//stack에 1을 push해줌
        stack.Push(2);
        stack.Push(3);

        if(stack.Count != 0){
        print(stack.Pop()); //stack에 맨 뒤에있는 것을 Pop해줌
        }
    }
}
728x90

'유니티로 배우는 c#' 카테고리의 다른 글

구조체  (0) 2021.10.10
네임스페이스  (0) 2021.10.10
지정자  (0) 2021.09.30
함수  (0) 2021.09.30
자료형  (0) 2021.09.30