Results 1 to 5 of 5

Thread: How can you remove and insert an object in the array list ?

  1. #1
    Join Date
    Dec 2010
    Posts
    66

    How can you remove and insert an object in the array list ?

    I have got a issue related with programming, i am a student by profession, I was just having an overview of the stack and linked list in the C++ language, but later on my friend informed me to use Array list instead of stack and linked list, you can use it in all the application such as console applications, windows applications and class libraries, so I opted for the Array list feature of the C# , but I am stuck in between , as I can insert the objects into it , but I am not able to do any sort of modifications into the array list of the class, I have been told that there are certain methods and properties that you can use along with the this but I am finding lots of difficulties as I am getting confused as it was the same as in the Visual basic , since it too required the Microsoft's Visual studio. If anyone of you having any sort of knowledge regarding this then please let me know as I do not like to open any sort of book, I look to learn online .

  2. #2
    Join Date
    May 2009
    Posts
    511

    Re: How can you remove and insert an object in the array list ?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    
    
      class Program
                     {
                            
    
      static void Main(string[] args)
                                 {
    
    
    
    int i = 0;
                ArrayList IList = new ArrayList();
                IList.Add("Item4");
                IList.Add("Item5");
                IList.Add("Item2");
                IList.Add("Item1");
                IList.Add("Item3"); 
                Console.WriteLine("Shows Added Items");
                for (i = 0; i <=IList.Count - 1; i++)
                {
                    Console.WriteLine(IList[i]);
                    
                }
                Console.WriteLine(" List before sorting");
                //insert an item 
                IList.RemoveAt(3);
                IList.Insert(3, "Item6");
                IList.RemoveRange(1,3);
    
                Console.WriteLine(" List after sorting");
    
                for (i = 0; i <= IList.Count - 1; i++)
                {
                    Console.WriteLine(IList[i]);
                }
    The above program shows the program for Array list and the methods and properties being used into it , so you can just take a look on he methods that are RemoveAt and insert methods are listed over there that can be used to remove and insert items in the Array list.

  3. #3
    Join Date
    May 2009
    Posts
    529

    Re: How can you remove and insert an object in the array list ?

    ArrayList is one of the most supple data structure in the collection of CSharp library. Collections. ArrayList class has a uncomplicated list of items. ArrayList makes the use of the IList interface that utilizes an array and very without difficulty we can add , insert , delete , and also access the view etc. It is very efficient because you can append and you need not mention any add on information about the size of the array list , because it will dynamically increase and shrink as you make any sort of change in it , so it is always advisable for you to make use of such data structure than opting for link list and stack.

  4. #4
    Join Date
    May 2009
    Posts
    637

    Re: How can you remove and insert an object in the array list ?

    If you want to add any object in the array list you can make the use of add method , it will add a new object in the current array list.

    Code:
    Syntax : ArrayList.add(object)
      object : Here the object means the item that is needed to be added in the currently created array list .
      ArrayList a;
      a.Add("i1");
    Here in the given example the array list a is the object of the array list class and it can be later on used to add object in the array list. In here an object called i1 is added in the array list. Here by default it will append in the last position of the array list.

  5. #5
    Join Date
    May 2009
    Posts
    527

    Re: How can you remove and insert an object in the array list ?

    Let me tell you that inserting and adding are two different tasks in the array list , adding the items or objects will append in the last position of the array list,
    Syntax : ArrayList.insert(index,object)
    index : Index specifies the the position of the item to be inserted in the ArrayList
    object : Object means the item to be added in the array list.

    We will take an example in here suppose you are creating any object b of the
    ArrayList then you can insert as shown in the below example.
    b.Insert(3, "Itemname");

    here the object will be inserted in the third index positon of the arraylist.

Similar Threads

  1. SqlException: Cannot insert duplicate key row in object 'dbo.tbDep
    By Leo Pold in forum Server Update Service
    Replies: 3
    Last Post: 07-01-2014, 09:51 AM
  2. Replies: 6
    Last Post: 14-05-2011, 11:06 PM
  3. Choose from a list of variables or array
    By AZUL in forum Software Development
    Replies: 4
    Last Post: 05-01-2010, 01:54 PM
  4. Add new contacts to an existing array list
    By Carnie in forum Software Development
    Replies: 3
    Last Post: 28-11-2009, 01:11 PM
  5. Cannot insert object in excel 2007
    By Darpak in forum Windows Software
    Replies: 3
    Last Post: 04-08-2009, 07:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,574,769.65511 seconds with 17 queries