List
- 생성자
var list = new List<int>();
list : 비어있음
var listA = new List<int>() {3, 2, 1};
listA : 3, 2, 1
var listB = new List<int>(listA);
listB = 3, 2, 1
var list = new List<int>(10);
list.Count : 0
list.Capacity : 10
- List.Add()
:리스트뒤에 요소 추가
list : 3, 2, 1
list.Add(4);
list : 3, 2, 1, 4
- List.AddRange()
: 리스트에 다른 리스트의 요소들을 뒤에 추가
listA : 3, 2, 1
listB : 4, 5
listA.AddRange(listB);
listA : 3, 2, 1, 4, 5
- List.Clear()
리스트 안에있는 모든 요소들 다 지워버리기
- List.Contains()
: 해당 요소가 있는지 여부 확인 (Bool값)
list : 3, 2, 1
list.Contains(1) => true;
list.Contains(4) => flase;
- 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()
: 다른 리스트를 복사해서 넣는다
//array에 List를 복사해서 넣는다.
list : 1, 2, 3
array : 0, 0, 0, 0, 0
list.CopyTo(array);
array : 1, 2, 3, 0, 0
//array에 복사한 List를 array의 2번째 인덱스부터 넣는다.
list : 1, 2, 3
array : 0, 0, 0, 0, 0
list.CopyTo(array, arrayIndex: 2);
array : 0, 0, 1, 2, 3
//list의 1번부터 복제하여 array에 3번째 인덱스 부분에 count개수만큼 복사해서 넣는다.
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.Exists()
: 리스트에 존재하는 요소들의 여부를 확인
마치 함수를 만들어 if로 검사하는 느낌이다.
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()
:
list : 1, 2, 3
list.ForEach(x => { Console.Write(x); });
output : 832
- List.Insert()
: 중간에 요소 삽입
//index 부분에 item을 넣는다.
list : 1, 2, 3
list.Insert(index: 1, item: 5);
list : 1, 5, 2, 3
- List.InsertRange()
: 중간에 collection의 모든 요소를 삽입
//index 부분에 listB에 있는 요소를 전부 삽입
listA : 1, 2, 3
listB : 4, 5
listA.InsertRange(index: 1, collection: listB);
listA : 1, 4, 5, 2, 3
- List.Remove()
: 특정 요소 삭제
중복된 요소가 있어도 최초의 첫번째 요소를 삭제
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()
: 리스트에 있는 요소의 순서를 바꾼다.
//리스트에 있는 요소의 순서를 거꾸로 다 바꿔버린다.
list : 1, 2, 3
list.Reverse();
list : 2, 3, 1
//Index 부분에서 count 개수만큼 순서를 거꾸로 바꿔버린다.
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