전체 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
예
다른 예를 보겠습니다 -
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