Remove the element from array

N

None

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
(e-mail address removed)
 
G

Greg Young

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#
 
V

V

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
 
G

Guest

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 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.
 
N

Nick Hounsome

None said:
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 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top