.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Remove an Item from ArrayList

Posted By:abhays       Posted Date: March 29, 2014    Points: 40    Category: Collections    URL: http://www.dotnetspark.com  

Below Code snnipets explain how to remove an element from ArrayList
 


The ArrayList class allows you to store values of different data types and also gives you the ability to add and remove elements anytime you want. The following examples show a simple functionality of theArrayList class.

You can use the Remove method of the ArrayList class to remove elements. The Remove method accepts an object that matches the value of an element in an array. It will remove the first occurance of the element that has that value. If you remove an element that is not in the last position, any element following it will adjust their position so that the empty position that the removed element was used to be will be occupied. This is demonstrated by the following code.
ArrayList arrayList = new ArrayList();
 
            arrayList.Add(9);
            arrayList.Add(10);
            arrayList.Add(11);
            arrayList.Add(22);
            arrayList.Add(44);
	arrayList.Add(95);
	arrayList.Add(35);
 
            for (int i = 0; i < arrayList.Count; i++)
            {
                Console.WriteLine("arrayList[{0}] = {1}", i, arrayList[i]);
            }
 
            //Remove element number 1
            arrayList.Remove(95);
 
            Console.WriteLine("\nAfter removing 95 from array List...\n");
 
            for (int i = 0; i < arrayList.Count; i++)
            {
                Console.WriteLine("arrayList[{0}] = {1}", i, arrayList[i]);
            }



     

Further Readings:

Responses
Author: abhays         Company URL: http://www.dotnetspark.com
Posted Date: March 29, 2014

Ideal Post for ArrayList

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend