티스토리 뷰

하... 몇개 언어를 해야하는건지 

 

Arrays(배열) 

 

배열 데이터 구조에 형식이 동일한 변수를 여러 개 저장

동적으로 할당이 불가

연속된 메모리 공간으로 이루어져 있기 때문에 관리가 용이하고, Index로 이루어져 있기 때문에 빠르게 해당값을 찾을 수 있음

요소의 형식을 지정하여 배열을 선언

숫자 배열 요소의 기본값은 0으로 설정되고, 참조 요소는 null로 설정

 

기본배열

int[] array1 = new int[5];
int[,] multiDimensionalArray1 = new int[2, 3];

 

다차원배열 

int[][] jaggedArray = new int[3][];

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
int [] arr = { 1, 2, 3, 4, 5} 
arr[2] = 100;
Console.WriteLine(string.Join(",",arr)); // 1, 2 100, 4,5

배열의 길이 가져오기

int[] arr = {1,2,3,4,5};
Console.WriteLint(arr.Length); // 5

 

 

 

배열 요소 제거

- C#에는 기존 배열에 요소를 제거하거나 추가 하는방법이 없음, 요소를 제거하거나 추가하려면 배열 대신 목록(List)를 사용하는 것이 좋음 

- 하지만 LINQ의 where을 사용해서 새 배열을 변환하는 등의 방법을 사용할 수 있음  

 

 

 

배열 초기화 및 Resize 

Array.Clear(arr,0,3); // 0부터 3까지 0으로 초기화
Arry.Resize(ref arr, arr.Length + 95);

 

 

 

                                          

System.Collections 클래스

 

 

 

System.Collecions.Generic 

 

 

Class name Description
Dictionary<TKey,TValue> It stores key/value pairs and provides functionality similar to that found in the non-generic Hashtable class.
List<T> It is a dynamic array that provides functionality similar to that found in the non-generic ArrayList class.
Queue<T> A first-in, first-out list and provides functionality similar to that found in the non-generic Queue class.
SortedList<TKey,TValue> It is a sorted list of key/value pairs and provides functionality similar to that found in the non-generic SortedList class.
Stack<T> It is a first-in, last-out list and provides functionality similar to that found in the non-generic Stack class.
HashSet<T> It is an unordered collection of the unique elements. It prevent duplicates from being inserted in the collection.
LinkedList<T> It allows fast inserting and removing of elements. It implements a classic linked list.

 

List

- 동적으로 할당이 가능하다 

- 포인터를 사용해서 다음 데이터의 주소값을 가지고 있기에 데이터 추가, 삭제가 편리 

 

var newList = new List<string>();
newList.Add("Apple");
newList.Add("Banana");
newList.Add("WaterMelon");
newList.Remove("Banana");
// 출력 string.Join(,) 사용 
Console.WriteLine(string.Join(",", newList)); // Apple, WaterMelon 

// 출력 ForEach 사용
newList.ForEach((i) => Console.WriteLine(i));

 

Dictionary 

- Dictionary <Key 데이터형, Value 데이터형> Object명 = new Dictionary .... 

Dictionary<string, Person> person = new Person<string, Person>();
person.Add("Rosy", new Person(20, "010-777-0000"));
person.Add("Jay", new Person(31, "090-777- 3910"));

 

var anotherDic = new Dictinary<String, Person>(){
	{"Rosy", new Person(20, "010-777-0000")},
    {"Jay", new Person(31, "090-777- 3910")}
}

 

 

 

System.Collection 

Class name Description
ArrayList It is a dynamic array means the size of the array is not fixed, it can increase and decrease at runtime.
Hashtable It represents a collection of key-and-value pairs that are organized based on the hash code of the key.
Queue It represents a first-in, first out collection of objects. It is used when you need a first-in, first-out access of items.
Stack It is a linear data structure. It follows LIFO(Last In, First Out) pattern for Input/output.

 

 

ArrayList 

ArrayList list = new ArrayList();
list.Add(10);
list.Add(20);
list.Add(30);

list.RemoveAt(1);
list.Insert(2, 39);
            
for(int i=0; i<list.Count; i++)
{
    Console.WriteLine(list[i]); //10,30,39

}

 

 

 

 

 

 

 

System.Collections.Concurrent 

여러 스레드에서 컬렉션 항목에 액세스하기 위한 효율적이고 스레드로부터 안전한 작업을 제공  

 

         

 

Class name Description
BlockingCollection It provides blocking and bounding capabilities for thread-safe collections that implement
IProducerConsumerCollection.
ConcurrentBag It represents a thread-safe, an unordered collection of objects.
ConcurrentDictionary It represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently.
ConcurrentQueue It represents a thread-safe first in-first out (FIFO) collection.
ConcurrentStack It represents a thread-safe last in-first out (LIFO) collection.
OrderablePartitioner It represents a particular manner of splitting an orderable data source into multiple partitions.
Partitioner It provides common partitioning strategies for arrays, lists, and enumerables.
Partitioner It represents a particular manner of splitting a data source into multiple partitions.

                                                                                                 

                                                                                                                                       

 

 

 

<출처>                                                                                                                                                1. Collections in C# - GeeksforGeeks

2. 컬렉션(C#) | Microsoft Docs 

3. 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함