네임스페이스 | 이름 |
---|---|
System | Array |
System.Collections | Stack, Queue, ArrayList |
System.Collections.Generic | List<T>, LinkedList<T>, Stack<T>, Queue<T> |
Array.Sort(arr); //정렬
Array.Reverse(arr); //배열의 요소를 역순으로 변환
Array.ConvertAll(arr, int.Parse); //변환 함수 일괄적용
스택은 탄창이라고 생각하면 된다.
Stack stack = new Stack();
stack.Push();
stack.Peek();
stack.Pop();
큐는 터널이라고 생각하면 된다.
Queue queue = new Queue();
queue.Enqueue();
queue.Dequeue();
stack.Count나 queue.Count는, Pop 이나 Enqueue 등을 통해 값이 변할 수 있으므로
int stack_size = stack.Count; <- 이런 식으로 변수에 저장해 사용하는 것이 좋다.
스택이나 큐를 사용할 때 try 문 내에서 스택이나 큐가 비어있게 되면, catch 문에서 예외 처리 메시지로 '스택(큐)이 비어있습니다.'라는 메시지를 출력할 수 있습니다.
제네릭 컬렉션의 딕셔너리랑 비슷하지만, 보통 해시테이블보다 딕셔너리를 많이 사용한다.
Hashtable hashtable = new Hashtable();
Stack<int> i_stack = new Stack<int>();
List<double> d_list = new List<double>();
d_list.Add(30.409f);
int size = 10;
var numbers = Enumerable.Range(1, size); //(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
var same_numbers = Enumerable.Repeat(0, size); //(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
IDictionary<string, string> dict = new Dictionary<string, string>();
dict.Add(key, value);
dict.Remove(key);
dict[key] = value;
dict.ContainsKey(key) : true, false 리턴
dict.TryGetValue(key, out string value) : value 변수에 값 가져오기
foreach(KeyValuePair<string, string> item in dict) : Pair로 가져오기