6
Reply

In an ArrayList, if an item is added using Insert method like below- al.Insert(2, "item") What will happen to the existing item at index 2?

Sandeep Soni

Sandeep Soni

6y
3.4k
0
Reply

    It slides to the next position at index 3.

    It will shift to the next index of that ArrayList.

    Please follow the given example below

    ArrayList a1 = new ArrayList();
    a1.Add(“abc”);
    a1.Add(“aa”);
    a1.Add(“baba”);

    //before inserting new element
    Console.WriteLine(a1[2]);

    //adding new element at 2nd index
    a1.Insert(2, “item”);

    //after added
    Console.WriteLine(a1[2]);
    Console.WriteLine(a1[3]);
    //“baba” is shifted at index 3 now

    Console.ReadLine();

    It shifted records to other index after index 2, That means it shift 2 index record to 3 and 3 to 4 and so on.

    will be at index 3 position

    After you add item to position 2, it will move existing item to position 3.

    item at position 2 will shift down