Groups | Blog | Home
all groups > c# > may 2006 >

c# : Remove the element from array


None
5/9/2006 10:42:11 PM
Hi,

I have declared array as Int[] ids = new int[50];

In ArrayList we can remove specified index using RemoveAt(5)
method. My question is how can we do this one with int array (single
dimensional array) or else is there any alternative solution for this?

If anybody knows the solution pls let me know.

Thanks and Regards,
Vinothkumar B
bvinoth@tvsinfotech.com
V
5/9/2006 11:27:28 PM
You could of course write a wrapper over your array which would move
each of the items below the deleted item one step up.

This would be VERY ineffcient of course, but depends on your
requirements.

Regards,
Vaibhav
Altaf Al-Amin Najwani
5/9/2006 11:35:01 PM
Interesting ... Microsoft Implements ArrayList as internal array of object
references to your value or referene types. As ArrayList maintains references
so removeat(5) means that object reference will be done null and that
particular element is finalized and references are refreshed that is
arraylist[i] references to arraylist[i+1]. Its very easy to do if array is
object array ... but if you are creating an array of like int or string (any
value type ) then its very tedious.

[quoted text, click to view]
Greg Young
5/10/2006 2:21:32 AM
You can't remove an item from an array you could say set the position to
null for an array of a reference type .. Arrays are a fixed size data
structure ..

when you define an array of int [50] think about it creating 50 ints one
after another .. the array points to the first 1 ... so array[22] points to
the memory address of array[0] + 22 * size of your data. In order to remove
an index you would have to recreate the array

Cheers,

Greg Young
MVP - C#
[quoted text, click to view]

Nick Hounsome
5/10/2006 7:04:08 AM

[quoted text, click to view]

If arrays could do everything ArrayList does then there would be no need for
ArrayList.

Removing stuff from an array is never going to be efficient however you do
it so just use ArrayList and ArrayList.ToArray

AddThis Social Bookmark Button