ArrayList Indexer

In this chapter you will learn:

  1. Using Indexer of an ArrayList

Using Indexer of an ArrayList

The following code uses Indexer to change contents.

using System; /*from  j a  v a 2s.c o  m*/
using System.Collections; 
 
class MainClass { 
  public static void Main() { 
    ArrayList al = new ArrayList(); 
     
    Console.WriteLine("Adding 6 elements"); 
    // Add elements to the array list 
    al.Add('C'); 
    al.Add('A'); 
    al.Add('E'); 
    al.Add('B'); 
    al.Add('D'); 
    al.Add('F'); 
 
    // 
    Console.WriteLine("Change first three elements"); 
    al[0] = 'X'; 
    al[1] = 'Y'; 
    al[2] = 'Z'; 
    Console.Write("Contents: "); 
    foreach(char c in al) 
      Console.Write(c + " "); 
    Console.WriteLine(); 
  } 
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to remove a range of elements from ArrayList
  2. Remove elements from ArrayList
  3. Clear an ArrayList