Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   About arrays (http://www.programmingforums.org/showthread.php?t=10935)

Samuaijack Aug 3rd, 2006 12:38 PM

About arrays
 
hey guys,
well i was just wndering if i can delete indexes from an array,for an example if i have a string entered by the user then i took every word in the string into an index of this array but if i have a few indexes in this array that remain null can i delete them ??

JimmyJim Aug 3rd, 2006 12:58 PM

Take a look at System.Collections.Generic namespace. There is List<T> which is far far more useful than just a standard Array. There are also other classes in that namepace that act differently but also might be useful.

You cant just remove an item on an array. You have to copy it into a new array up to the point where you want to remove. In your case you would check for nulls to find out where you would stop. You then skip the null index and the continue copying the rest of the array checking for more nulls. I would post some code but you will learn more if you figure it out.

Samuaijack Aug 3rd, 2006 5:35 PM

thnx jimmy i will try that.

kurifu Aug 5th, 2006 5:19 PM

If you delete an index from an array there remains a gap there in the memory unless you write some sort of funtion to copy all data past the point backward one index to fill the gap. Same behaviour as you would expect in C++, and well many languages I am sure.

List<T> is a great solution to this since it provides funtions which will do exactly that for you!

Oh of course the othe option too is to use something like a linked list which would be faster because your wouldn't be copying data around your memory so much, you just have to update the links between two of the indexes.


All times are GMT -5. The time now is 12:44 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC