C#
[유니티로 배우는 C#] 컬렉션
재코딩
2022. 7. 19. 10:36
반응형
컬렉션
배열의 크기에 대한 단점을 보완해주는 집합체
ArrayList: 연산량이 많아 과부하가 걸릴 수 있다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Unity_lecture : MonoBehaviour
{
//배열의 크기가 5로 고정
int[] exp = new int[5] {1, 2, 3, 4, 5};
//컬렉션: 리스트, 큐, 스택, 해시테이블, 딕셔너리, 어레이리스트
//컬렉션을 사용하기 위해서는 using System.Collections;를 head에 넣어주어야함
ArrayList arrayList = new ArrayList();
void Start(){
// Add: 추가 메소드
arrayList.Add(1);
arrayList.Add(2);
arrayList.Add(3);
arrayList.Add("ABCDE");
arrayList.Add(4.4);
print(arrayList.Count); //arrayList의 길이를 출력 5
for (int i = 0; i <arrayList.Count; i ++){
print(arrayList[i]);
}
// Remove: 삭제 메소드
arrayList.Remove("ABCDE"); // ABCDE 삭제
arrayList.RemoveAt(0); // 1삭제
arrayList.RemoveRange(2, 3); // index 2~3 삭제
// Clear: 초기화
arrayList.Clear();
// CopyTo: 복사
// Contains: 특정 값이 있는지 bool값으로 return해준다.
print(arrayList.Contains("ABCDE")); //False가 반환된다. 위에서 삭제시켜주었기 때문이다.
//Insert: index, value
arrayList.Insert(1, 5); // 2번째에 5를 추가하라.
}
}
List: 특정 자료형만 사용가능하다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Unity_lecture : MonoBehaviour
{
//배열의 크기가 5로 고정
int[] exp = new int[5] {1, 2, 3, 4, 5};
//컬렉션: 리스트, 큐, 스택, 해시테이블, 딕셔너리, 어레이리스트
//컬렉션을 사용하기 위해서는 using System.Collections;를 head에 넣어주어야함
ArrayList arrayList = new ArrayList();
// 특정 자료형만 사용 가능하다.
List<int> list = new List<int>();
void Start(){
list.Add(3);
list.Add(245);
}
}
Hashtable, Dictionary: dictionary가 hashtable보다 연산에서 유리하다
hashtable: 자료형 명시 x
dictionary: 자료형 명시 o
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Unity_lecture : MonoBehaviour
{
//배열의 크기가 5로 고정
int[] exp = new int[5] {1, 2, 3, 4, 5};
//컬렉션: 리스트, 큐, 스택, 해시테이블, 딕셔너리, 어레이리스트
//컬렉션을 사용하기 위해서는 using System.Collections;를 head에 넣어주어야함
ArrayList arrayList = new ArrayList();
// 특정 자료형만 사용 가능하다.
List<int> list = new List<int>();
Hashtable hashtable = new Hashtable();
Dictionary<string, int> dictionary = new Dictionary<string, int>();
void Start(){
// hashtable은 python의 dictionary와 비슷하게 key값과 value가 있다. (자료형 명시 x)
hashtable.Add("만", 10000);
hashtable.Add("백만", 1000000);
hashtable.Add(50, "오십");
print(hashtable["만"]);
print(hashtable["백만"]);
print(hashtable[50]);
// dictionary: 연산에서 hashtable보다 유리하다.
dictionary.Add("가", 100);
}
}
Queue, Stack: FIFO, LIFO
Queue: First Input First Output
Stack: Last Input First Output
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Unity_lecture : MonoBehaviour
{
// Queue: FIFO, 선입선출
Queue<int> queue = new Queue<int>();
// Stack, LIFO, 후입선출
Stack<int> stack = new Stack<int>();
void Start(){
// 추가하기
queue.Enqueue(5);
queue.Enqueue(10);
// 꺼내보기
// queue에 element가 있는지 체크
if(queue.Count != 0){
print(queue.Dequeue()); // 5출력
print(queue.Dequeue()); // 10출력
}
//추가하기
stack.Push(1);
stack.Push(2);
stack.Push(3);
if (stack.Count != 0){
print(stack.Pop()); // 3출력
print(stack.Pop()); // 2출력
print(stack.Pop()); // 1출력
}
}
}
강의자료: 케이디 유튜브
반응형