Inserting into an ArrayList by index : ArrayList « Collections Data Structure « C# / C Sharp






Inserting into an ArrayList by index

  


using System;
using System.Collections;

class MainClass {

    public static void Main() {
        ArrayList al = new ArrayList(5);

        // Add three elements to the end of the array
        al.Add(10);
        al.Add(9);
        al.Add(8);

        // Now, insert three elements in the front of the array
        al.Insert(0, 1);
        al.Insert(0, 2);
        al.Insert(0, 3);

        // Finally, insert into some random spots
        al.Insert(2, 4);
        al.Insert(4, 5);
        al.Insert(6, 6);


        // Enumerate the array
        foreach (int i in al) {
            Console.WriteLine("Entry {0}", i);
        }

    }
}

           
         
    
  








Related examples in the same category

1.Add items to ArrayList and use foreach loop to check
2.Add array to ArrayList
3.Remove range from ArrayList
4.Clone an ArrayList
5.CopyTo, ToArray(), ToArray(typeof(String))
6.Using foreach to loop through ArrayList and use Indexer of ArrayList
7.Check InvalidCastException when using foreach loop with ArrayList
8.Binary Search an ArrayList
9.Checks time needed for list operations using an ArrayList implementationChecks time needed for list operations using an ArrayList implementation
10.ArrayList Demo: hold classArrayList Demo: hold class
11.illustrates the use of ArrayList properties and methodsillustrates the use of ArrayList properties and methods
12.illustrates the use of an ArrayList that contains objects of the Car classillustrates the use of an ArrayList that contains objects of the Car class
13.illustrates the use of ArrayLists 2illustrates the use of ArrayLists 2
14.Convert an ArrayList into an arrayConvert an ArrayList into an array
15.Sort and search an ArrayListSort and search an ArrayList
16.Demonstrate ArrayListDemonstrate ArrayList
17.Search for the first occurrence of the duplicated value
18.Search for the first occurrence of the duplicated value in the last section of the ArrayList
19.Search for the first occurrence of the duplicated value in a section of the ArrayList
20.Search for the first occurrence of the duplicated value in a small section at the end of the ArrayList
21.Convert a ArrayList object to a array
22.Represents a loose abstraction of an arraylist, wheres the buffer array is available for public access.