C#
[C#] 리스트 주요 함수 정리
개발자엄지희
2023. 4. 19. 10:58
반응형
List?
List<제네릭> list = new List<제네릭>();
보통 이런 식으로 생성한다.
리스트는 <제네릭> 키워드로 해당 타입의 형식을 담는다.
[C#] List 주요 함수 정리
출처 http://www.csharp-examples.net 생성자 var list = new List(); list : (empty) var listA = new List() {3, 2, 1}; listA : 3, 2, 1 var list = new List(listA); listB : 3, 2, 1 var list = new List(10); list.Count : 0 list.Capacity : 10 List[index] list
tenlie10.tistory.com
생성자 : 리스트를 생성하는 여러 가지 방법
var list = new List<int>();
list : (empty)
var listA = new List<int>() {3, 2, 1};
listA : 3, 2, 1
var list = new List<int>(listA);
listB : 3, 2, 1
var list = new List<int>(10);
list.Count : 0
list.Capacity : 10
List[index] : 인덱스로 리스트 항목 꺼내기
list : 3, 2, 1
int item = list[1];
item : 3
list : 3, 2, 1
list[1] = 4;
list : 3, 4, 1
List.Add : 리스트에 항목 추가
list : 3, 2, 1
list.Add(4);
list : 3, 2, 1, 4
List.AddRange: 리스트 합치기
listA : 1, 2, 3
listB : 4, 5
listA.AddRange(listB);
listA : 1, 2, 3, 4, 5
List.Clear : 리스트 항목 전체 삭제
list : 1, 2, 3
list.Clear();
list : (empty)
List.Contains: 리스트 안에 항목이 들어있는지 확인
list : 1, 2, 3
list.Contains(1) => true
list.Contains(4) => false
List.ConvertAll : 리스트 전체 요소에 특정 작업을 추가해서 반환 (깊은 복사)
// listA : 1, 2, 3
var conv = new Converter<int, decimal>(x => (decimal)(x+1));
var listB = listA.ConvertAll<decimal>(conv);
// listB : 2.0, 3.0, 4.0
List.CopyTo: 리스트를 복사 ([index], array, [arrayIndex], [count]) (얕은 복사)
list : 1, 2, 3
array : 0, 0, 0, 0, 0
list.CopyTo(array);
array : 1, 2, 3, 0, 0
list : 1, 2, 3
array : 0, 0, 0, 0, 0
list.CopyTo(array, arrayIndex: 2);
array : 0, 0, 1, 2, 3
list : 1, 2, 3
array : 0, 0, 0, 0, 0
list.CopyTo(index: 1, array: array, arrayIndex: 3, count: 1);
array : 0, 0, 0, 2, 0
List.Distinct: 리스트 내 중복 제거
List<int> list = new List { 1, 2, 2, 3 };
list.Distinct().ToList();
// list: 1, 2, 3
https://developer-talk.tistory.com/215
[C#]배열 중복 값 제거(Distinct)
배열에서 중복 값을 제거한다는 의미는 고유한 값만 가진다는 의미입니다. 하지만, C#에서는 배열의 값을 제거할 수 없기 때문에 중복 값이 없는 새로운 배열을 생성해야 합니다. 이러한 경우 Dis
developer-talk.tistory.com
List.Exists: 리스트 내 특정 항목 존재 확인
list : 1, 2, 3
list.Exists(x=> x==3); => true
list.Exists(x=> x>10); => false
List.Equals: 동일한 객체인지 판단
var listA = new List<int>() {1,2,3};
var listB = listA;
listA.Equals(listB); => true
var listA = new List<int>() {1,2,3};
var listB = new List<int>() {1,2,3};
listA.Equals(listB); => false
List.Find: 배열 내 특정 항목 찾아서 해당 항목의 값을 반환
list : 1, 2, 3
int item = list.Find(x => x>2);
item : 3
list : 1, 2, 3
int item = list.Find(x => x>10);
item : 0
List.FindAll: 배열 내 특정 항목 찾아서 모두 반환
listA : 1, 2, 3
var listB = listA.FindAll(x => x>1);
listB : 2, 3
listA : 1, 2, 3
var listB = listA.FindAll(x => x>10);
listB : (empty)
List.ForEach:
var list = new List<int>() {1, 2, 3, 4};
list.ForEach(i =>
{
if (i == 3)
return;
Console.WriteLine(i);
}
);
List.Insert: 특정 위치에 특정 항목 삽입 (index, item)
list : 1, 2, 3
list.Insert(index: 1, item: 5);
list : 1, 5, 2, 3
List.InsertRange: 특정 위치에 특정 항목들 삽입 (index, collection)
listA : 1, 2, 3
listB : 4, 5
listA.InsertRange(index: 1, collection: listB);
listA : 1, 4, 5, 2, 3
List.Remove: 특정 조건을 만족하는 최초의 항목 삭제 (item)
list : 1, 4, 2, 4
list.Remove(item: 4);
list: 1, 2, 4
List.RemoveAll: 특정 조건을 만족하는 모든 항목 삭제
list : 1, 2, 3, 4
list.RemoveAll(x => x<3);
list : 3, 4
List.RemoveAt: 특정 인덱스의 항목 삭제 (index)
list : 1, 2, 3, 4
list.RemoveAt(index: 2);
list : 1, 2, 4
List.RemoveRange: 특정 인덱스부터 특정 개수의 항목 삭제 (index, count)
list : 1, 2, 3, 4, 5, 6
list.RemoveRange(index: 2, count:3)
list : 1, 2, 6
List.Reverse: 리스트 역순으로 바꾸기 ([index, count])
list : 1, 2, 3
list.Reverse();
list : 3, 2, 1
list : 1, 2, 3
list.Reverse(index: 1, count: 2);
list : 1, 3, 2
List.Sort: 리스트 정렬 함수
list : 1, 3, 2, 4
list.Sort(); => list : 1, 2, 3, 4
list.Sort((x,y) => x.CompareTo(y)); => list : 1, 2, 3, 4
list.Sort((x,y) => y.CompareTo(x)); => list : 4, 3, 2, 1
list.Sort(new MyComparer()); => list : 1, 2, 3, 4
public class MyComparer : IComparer<int>
{
public int Compare(int x, int y) { return x.CompareTo(y); }
}
List.ToArray: 리스트를 배열로 변환
list : 1, 2, 3
int[] array = list.ToArray();
array : 1, 2, 3
list : (empty)
int[] array = list.ToArray();
array : (empty)
List.TrimExcess: 쓰지 않는 여유공간 제거
list : 1,2,3,4,5,
list.Count : 5
list.Capacity : 8
list.TrimExcess();
list.Count : 5
list.Capacity : 5
딕셔너리
https://engineer-mole.tistory.com/174
[C#] C#의 Dictionary (사전형) 데이터 사용법
Dictionary이란 Dictionary에서는 Key라고 불리는 인덱스 번호를 대신해 사용하는 명칭과 Value라고 불리는 값을 세트로 다룬다. 참고로 Key와 Vlaue 세트로 다루는 배열을 "연관 배열"이라고 부른다. C#에
engineer-mole.tistory.com
반응형