C#에서 ArrayList의 요소 순서를 뒤집으려면 Reverse() 메서드를 사용하면 됩니다. 이 메서드는 두 가지 오버로드를 제공하며, 매개변수 없이 호출하면 목록 전체가 뒤집히고, 시작 인덱스와 개수를 지정하면 해당 범위의 요소만 뒤집힙니다.
- Reverse() – ArrayList의 모든 요소 순서를 반대로 변경합니다.
- Reverse(int index, int count) – 지정된 인덱스부터 지정된 개수만큼의 요소 순서만 반대로 변경합니다.
예제 1: ArrayList 전체 요소 순서 뒤집기
다음은 ArrayList에 저장된 모든 요소의 순서를 반대로 바꾸는 코드입니다.
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");
Console.WriteLine("Elements in ArrayList2...");
foreach (string res in list2) {
Console.WriteLine(res);
}
ArrayList list3 = new ArrayList();
Console.WriteLine("Current capacity of ArrayList3: " + list3.Capacity);
list3 = list2;
Console.WriteLine("Current capacity of ArrayList3 (Updated) " + list3.Capacity);
Console.WriteLine("Is ArrayList3 equal to ArrayList2? = "+list3.Equals(list2));
Console.WriteLine("Count of elements in ArrayList2 = " + list2.Count);
list2.Reverse();
Console.WriteLine("Elements in ArrayList2...Reversed");
foreach (string res in list2) {
Console.WriteLine(res);
}
}
}출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
Elements in ArrayList1... A B C D E F G H I Elements in ArrayList2... A B C D E F G H I Current capacity of ArrayList3: 0 Current capacity of ArrayList3 (Updated) 16 Is ArrayList3 equal to ArrayList2? = True Count of elements in ArrayList2 = 9 Elements in ArrayList2...Reversed I H G F E D C B A
실행 결과를 보면 list2.Reverse() 호출 후 요소 순서가 A~I에서 I~A로 완전히 뒤집힌 것을 확인할 수 있습니다. 또한 list3 = list2처럼 참조를 할당하면 두 변수가 동일한 객체를 가리키므로 Equals() 비교 결과가 True로 반환되고, 새로 생성된 ArrayList의 기본 용량(Capacity)이 0에서 16으로 늘어나는 것도 함께 확인할 수 있습니다.
예제 2: 지정된 범위의 요소만 뒤집기
이번에는 Reverse(int index, int count) 오버로드를 사용해 특정 구간의 요소만 뒤집어 보겠습니다. 아래 예제는 인덱스 1부터 3개의 요소 순서를 반대로 바꿉니다.
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
ArrayList list = new ArrayList();
list.Add("Andy");
list.Add("Gary");
list.Add("Katie");
list.Add("Amy");
Console.WriteLine("Elements in ArrayList...");
foreach (string res in list) {
Console.WriteLine(res);
}
string[] strArr = { "John", "Jacob" };
list.AddRange(strArr);
Console.WriteLine("Elements in ArrayList...UPDATED");
foreach(String str in list) {
Console.WriteLine(str);
}
list.Reverse(1, 3);
Console.WriteLine("Elements in ArrayList2...Reversed 3 elements beginning from index 1");
foreach (string res in list) {
Console.WriteLine(res);
}
}
}출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
Elements in ArrayList... Andy Gary Katie Amy Elements in ArrayList...UPDATED Andy Gary Katie Amy John Jacob Elements in ArrayList2...Reversed 3 elements beginning from index 1 Andy Amy Katie Gary John Jacob
list.Reverse(1, 3)을 호출하면 인덱스 1~3에 위치한 Gary, Katie, Amy가 Amy, Katie, Gary로 뒤집히고, 나머지 요소들은 원래 위치 그대로 유지됩니다.
정리
ArrayList의 Reverse() 메서드는 별도의 정렬 로직 없이 요소 순서를 간단하게 반전할 수 있는 유용한 기능입니다. 목록 전체를 뒤집을 때는 매개변수 없이 호출하고, 일부 구간만 뒤집을 때는 시작 인덱스와 요소 개수를 인자로 전달하면 됩니다.