Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C# List에서 요소 범위 제거하기 – RemoveRange() 메서드 활용법

C# List에서 요소 범위 제거하기

C#의 List<T> 컬렉션에서 연속된 여러 요소를 한 번에 제거하려면 RemoveRange() 메서드를 사용합니다. 이 메서드는 시작 인덱스와 제거할 요소의 개수를 매개변수로 받아 해당 범위의 요소들을 일괄 삭제합니다.

메서드 구문: void RemoveRange(int index, int count)

  • index: 제거를 시작할 인덱스(0부터 시작)
  • count: 제거할 요소의 개수

예제 1: RemoveRange 기본 사용법

다음 예제에서는 두 개의 리스트를 생성한 뒤, list2에서 인덱스 1부터 3개의 요소를 제거하는 과정을 보여줍니다.

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(String[] args){
      List<string> list1 = new List<string>();
      list1.Add("One");
      list1.Add("Two");
      list1.Add("Three");
      list1.Add("Four");
      list1.Add("Five");
      Console.WriteLine("List1의 요소...");
      foreach (string res in list1){
         Console.WriteLine(res);
      }
      List<string> list2 = new List<string>();
      list2.Add("India");
      list2.Add("US");
      list2.Add("UK");
      list2.Add("Canada");
      list2.Add("Poland");
      list2.Add("Netherlands");
      Console.WriteLine("List2의 요소...");
      foreach (string res in list2){
         Console.WriteLine(res);
      }
      Console.WriteLine("\nList2는 List1과 같은가요? = "+list2.Equals(list1));
      Console.WriteLine("\nlist2의 요소 개수 = "+list2.Count);
      list2.RemoveRange(1,3);
      Console.WriteLine("\nlist2의 요소 개수(업데이트 후) = "+list2.Count);
      Console.WriteLine("List2의 요소... 업데이트됨");
      foreach (string res in list2){
         Console.WriteLine(res);
      }
   }
}

실행 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

List1의 요소...
One
Two
Three
Four
Five
List2의 요소...
India
US
UK
Canada
Poland
Netherlands

List2는 List1과 같은가요? = False

list2의 요소 개수 = 6
list2의 요소 개수(업데이트 후) = 3
List2의 요소... 업데이트됨
India
Poland
Netherlands

결과 분석

list2에는 처음 6개의 요소가 있었습니다. RemoveRange(1, 3)을 호출하면 인덱스 1부터 3개의 요소, 즉 "US", "UK", "Canada"가 제거되고, 남은 요소는 "India", "Poland", "Netherlands"의 3개가 됩니다.

예제 2: RemoveRange를 여러 번 호출하기

이번에는 하나의 리스트에서 RemoveRange()를 두 번 호출하여 요소를 단계적으로 제거하는 예제를 살펴보겠습니다.

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(String[] args){
      List<string> list = new List<string>();
      list.Add("India");
      list.Add("US");
      list.Add("UK");
      list.Add("Canada");
      list.Add("Poland");
      list.Add("Netherlands");
      list.Add("Bhutan");
      list.Add("Singapore");
      Console.WriteLine("List의 요소...");
      foreach (string res in list){
         Console.WriteLine(res);
      }
      Console.WriteLine("\nlist의 요소 개수 = "+list.Count);
      list.RemoveRange(1,3);
      Console.WriteLine("\nlist의 요소 개수(업데이트 후) = "+list.Count);
      Console.WriteLine("List의 요소... 업데이트됨");
      foreach (string res in list){
         Console.WriteLine(res);
      }
      list.RemoveRange(2,3);
      Console.WriteLine("\nlist의 요소 개수(업데이트 후) = "+list.Count);
      Console.WriteLine("List의 요소... 업데이트됨");
      foreach (string res in list){
         Console.WriteLine(res);
      }
   }
}

실행 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

List의 요소...
India
US
UK
Canada
Poland
Netherlands
Bhutan
Singapore

list의 요소 개수 = 8

list의 요소 개수(업데이트 후) = 5
List의 요소... 업데이트됨
India
Poland
Netherlands
Bhutan
Singapore
list의 요소 개수(업데이트 후) = 2
List의 요소... 업데이트됨
India
Poland

결과 분석

처음 8개의 요소가 있던 리스트에서 첫 번째 RemoveRange(1, 3) 호출로 "US", "UK", "Canada"가 제거되어 5개가 되었고, 두 번째 RemoveRange(2, 3) 호출로 "Netherlands", "Bhutan", "Singapore"가 제거되어 최종적으로 "India"와 "Poland"만 남게 됩니다.

핵심 정리

  • RemoveRange(index, count)는 지정한 인덱스부터 count개의 요소를 한 번에 제거합니다.
  • 요소가 제거되면 Count 속성 값이 줄어들고, 나머지 요소들은 자동으로 앞으로 이동합니다.
  • 인덱스나 범위가 유효하지 않으면 ArgumentOutOfRangeException이 발생하므로 주의해야 합니다.
  • 반복문으로 요소를 하나씩 제거하는 것보다 RemoveRange()를 사용하는 것이 성능 면에서 더 유리합니다.