none
How to remove array elements

    Question

  • Hi all,

     

    It is possible to remove array elements like the first 2 elements of each array?

     

     

     

     

    Thanks,

    Aw

    Sunday, August 12, 2007 7:10 AM

Answers

  • It doesn't change the length of the array, you will have to check for nulls in your code.

     

    Array is better for performance. For example you can find an element for a given index very fast - the length of the array does not matter. With list you can add and remove elements from any position, and do iterations on the list elements (using foreach loop)

    Sunday, August 12, 2007 8:19 AM
  • Use Array.Copy and then Array.Resize.  See this thread:  Remove an object from the middle of an array.

    Sunday, August 12, 2007 8:38 AM
  • Yes, there is a performance penalty to using Resize, it must allocate a new array and copy the elements from the source array to the new array.  The original array will then be using memory until the next GC occurs.

     

    If you have a collection that you need to remove items from, I recommend using an indexable collection that supports element removal with O(n) (linear) performance, like List<T>

    Sunday, August 12, 2007 2:42 PM
    Moderator
  • Hi

    No, it will not change the length of array. The length of array would be still the same.

    Thanks And Regards
    Anu Viswan

    www.AnuViswan.blogspot.com | www.mstechexperts.org
    Sunday, August 12, 2007 2:48 PM
  • A list is different than an array in that an array allocates a contiguous chunk of memory to store the data.  A list, depending upon implementation, allocates chunks of data as time goes on.  The default for List is to double its size whenever it runs out of space.  You can override this by specifying a size when you create the list and avoid having to reallocate chunks as you add data.  For example:

    Code Snippet

    List myList new List(10);

    // call add 10 times...

     

     

    The above would avoid having to reallocate the backing store for the list 3 times.  (starts with 0 elements, starts with a minimum of 4, then doubles afterwards.  So, if you know you will have 10 elements, List(10) will have roughly the same performance and footprint as int[10];

     

    You should always try to preallocate your list if you know or can estimate it's size.  The following is not efficient:

    Code Snippet

    List<int> list = new List<int>();

    list.Add(1);

    list.Add(2);

    list.Add(3);

    list.Add(4);

    list.Add(5);

    list.Add(6);

    list.Add(7);

    list.Add(8);

    list.Add(9);

    list.Add(10);

     

     

    Sunday, August 12, 2007 11:30 PM
    Moderator

All replies

  • You cannot remove elements from an array, you can set elements to null.

    You can use a list instead of an array and then u'll have the option to remove elements

    Sunday, August 12, 2007 7:33 AM
  •  

    Thanks for replying.

     

    If I set the first 2 elements to null, will it change the length of the array? If not, should I use list like you said? But I wonder what's the advantage and disadvantages of it over the array?

     

     

     

    Thanks again,

    Aw

    Sunday, August 12, 2007 8:13 AM
  • It doesn't change the length of the array, you will have to check for nulls in your code.

     

    Array is better for performance. For example you can find an element for a given index very fast - the length of the array does not matter. With list you can add and remove elements from any position, and do iterations on the list elements (using foreach loop)

    Sunday, August 12, 2007 8:19 AM
  • Use Array.Copy and then Array.Resize.  See this thread:  Remove an object from the middle of an array.

    Sunday, August 12, 2007 8:38 AM
  • Thanks for replying. Is there any performance hit using Array.Resize.

    And another thing I wonder is, why some array methods are used like this:

    arrayname.Method

    whereas others are like this:

    Array.Method




    Thanks alot,
    Aw
    Sunday, August 12, 2007 8:56 AM
  • Yes, there is a performance penalty to using Resize, it must allocate a new array and copy the elements from the source array to the new array.  The original array will then be using memory until the next GC occurs.

     

    If you have a collection that you need to remove items from, I recommend using an indexable collection that supports element removal with O(n) (linear) performance, like List<T>

    Sunday, August 12, 2007 2:42 PM
    Moderator
  • Hi

    No, it will not change the length of array. The length of array would be still the same.

    Thanks And Regards
    Anu Viswan

    www.AnuViswan.blogspot.com | www.mstechexperts.org
    Sunday, August 12, 2007 2:48 PM
  • Thanks alot guys.

    If list can offer such a functionality, it is slower, right? It basically does, copy and resize automatically?

    Also if I have a while loop that will create an array with n elements which I don't know beforehand, is there a way/trick to use arrays in that situation?

    I understand the point of using arrays so I will try to use arrays whenever I can.



    Thanks again,
    Aw
    Sunday, August 12, 2007 11:16 PM
  • A list is different than an array in that an array allocates a contiguous chunk of memory to store the data.  A list, depending upon implementation, allocates chunks of data as time goes on.  The default for List is to double its size whenever it runs out of space.  You can override this by specifying a size when you create the list and avoid having to reallocate chunks as you add data.  For example:

    Code Snippet

    List myList new List(10);

    // call add 10 times...

     

     

    The above would avoid having to reallocate the backing store for the list 3 times.  (starts with 0 elements, starts with a minimum of 4, then doubles afterwards.  So, if you know you will have 10 elements, List(10) will have roughly the same performance and footprint as int[10];

     

    You should always try to preallocate your list if you know or can estimate it's size.  The following is not efficient:

    Code Snippet

    List<int> list = new List<int>();

    list.Add(1);

    list.Add(2);

    list.Add(3);

    list.Add(4);

    list.Add(5);

    list.Add(6);

    list.Add(7);

    list.Add(8);

    list.Add(9);

    list.Add(10);

     

     

    Sunday, August 12, 2007 11:30 PM
    Moderator
  • Thanks Peter, very helpful reply!

    Now I know the difference.



    Aw
    Wednesday, August 15, 2007 2:29 AM
  • When I remove an element form a list how are the indexes adjusted?

     

    e.g.:

    I remove element with index 5 from list of 10 elements: (indexes):

    1,2,3,4,5,6,7,8,9,0

     

    will the indexes will be 1 to 9 with the shift left:

    6 -> 5

    7 -> 6 ...

     

    or will remain as they are?

     

    Thank you

    Sergey.

     

    Monday, September 03, 2007 10:19 AM
  • The indexes will be shifted as you showed it up in the example.

     

    Monday, September 03, 2007 1:41 PM