ArrayList Reverse

In this chapter you will learn:

  1. How to reverse elements in an ArrayList

Reverse elements

The following code uses the Reverse() method to reverse myArrayList.

using System;/*  ja  va 2  s.co  m*/
using System.Collections;

class MainClass
{
  public static void Main()
  {
    ArrayList myArrayList = new ArrayList();
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    

    string[] anotherStringArray = {"Here's", "some", "more", "text"};
    myArrayList.SetRange(0, anotherStringArray);

    myArrayList.Reverse();
    DisplayArrayList("myArrayList", myArrayList);

  }
  public static void DisplayArrayList(string arrayListName, ArrayList myArrayList)
  {
    for (int i = 0; i < myArrayList.Count; i++){
      Console.WriteLine(arrayListName + "[" + i + "] = " +
        myArrayList[i]);
    }
  }

}

Next chapter...

What you will learn in the next chapter:

  1. How to search an ArrayList
  2. How to search from the end of an ArrayList
  3. How to search within a range of an ArrayList