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

C# ArrayList에서 특정 개체의 첫 번째 항목만 제거하는 방법

개요

C#의 ArrayList에서 특정 개체가 처음으로 나타나는 위치의 항목을 제거하려면 Remove() 메서드를 사용합니다. 이 메서드는 지정한 개체와 일치하는 첫 번째 요소만 삭제하며, 동일한 값이 여러 개 존재하더라도 나머지 항목은 그대로 유지됩니다.

항목이 제거되면 뒤쪽에 있던 요소들이 자동으로 한 칸씩 앞으로 이동하고, Count 속성 값도 1만큼 감소합니다.

예제 1

다음 예제에서는 두 개의 ArrayList를 생성하고, 두 번째 리스트에 중복으로 포함된 값 중 첫 번째 항목을 Remove()로 제거하는 과정을 보여줍니다.

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args) {
      ArrayList list1 = new ArrayList();
      list1.Add("A");
      list1.Add("B");
      list1.Add("C");
      list1.Add("D");
      list1.Add("E");
      list1.Add("F");
      list1.Add("G");
      list1.Add("H");
      list1.Add("I");
      Console.WriteLine("Elements in ArrayList1...");
      foreach (string res in list1) {
         Console.WriteLine(res);
      }
      ArrayList list2 = new ArrayList();
      list2.Add("A");
      list2.Add("B");
      list2.Add("C");
      list2.Add("D");
      list2.Add("E");
      list2.Add("F");
      list2.Add("G");
      list2.Add("H");
      list2.Add("I");
      list2.Add("G");
      list2.Add("I");
      Console.WriteLine("Elements in ArrayList2...");
      foreach (string res in list2) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Count of elements in ArrayList2 = " + list2.Count);
      list2.Remove("G");
      Console.WriteLine("Elements in ArrayList2... (UPDATED)");
      foreach (string res in list2) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Count of elements in ArrayList2 (Updated) = " + list2.Count);
   }
}

출력 결과

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

Elements in ArrayList1...
A
B
C
D
E
F
G
H
I
Elements in ArrayList2...
A
B
C
D
E
F
G
H
I
G
I
Count of elements in ArrayList2 = 11
Elements in ArrayList2... (UPDATED)
A
B
C
D
E
F
H
I
G
I
Count of elements in ArrayList2 (Updated) = 10

출력 결과를 보면 list2에는 "G"가 두 번 포함되어 있었지만, Remove("G") 실행 후 앞쪽에 위치한 첫 번째 "G"만 제거되고 뒤쪽의 "G"는 그대로 남아 있는 것을 확인할 수 있습니다.

예제 2

이번에는 문자열 목록에서 특정 항목을 하나 제거하는 또 다른 예제를 살펴보겠습니다.

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args) {
      ArrayList list = new ArrayList();
      list.Add("Laptop");
      list.Add("Dektop");
      list.Add("Wearables");
      list.Add("Tablet");
      list.Add("Notebook");
      list.Add("Ultrabooks");
      Console.WriteLine("Elements in ArrayList...");
      foreach (string res in list) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Count of elements in ArrayList = " + list.Count);
      list.Remove("Tablet"); 
      Console.WriteLine("Elements in ArrayList... (UPDATED)");
      foreach (string res in list) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Count of elements in ArrayList (Updated) = " + list.Count);
   }
}

출력 결과

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

Elements in ArrayList...
Laptop
Dektop
Wearables
Tablet
Notebook
Ultrabooks
Count of elements in ArrayList = 6
Elements in ArrayList... (UPDATED)
Laptop
Dektop
Wearables
Notebook
Ultrabooks
Count of elements in ArrayList (Updated) = 5

핵심 정리

  • Remove(object)는 해당 개체가 처음 발견되는 위치의 요소만 제거합니다.
  • 제거하려는 개체가 목록에 없어도 예외가 발생하지 않으며, 리스트는 변경되지 않습니다.
  • 요소가 제거되면 이후 인덱스가 자동으로 재조정되고 Count가 1 감소합니다.
  • 일치하는 모든 항목을 제거하려면 RemoveAll() 패턴이나 반복 처리를 활용해야 합니다.