[.net] Removing elements from an array

Started by
4 comments, last by deathkrush 17 years, 6 months ago
I have an array of a certain number of sorted Int32s. I need to remove the lowest one from the array. I tried this: Int32[] intArray = new Int32[10]; //Assign values and sort Array.Clear(intArray, 0, 1); //This doesn't work :( but unfortunately, it doesn't work. Something screwy happens where (usually) the lowest number is set to zero instead of being actually removed from the array. What I want is to convert this, for example: 1, 1, 3, 4, 5 to 1, 3, 4, 5 or 3, 5, 7, 10 to 5, 7, 10 You get the idea. Thanks for any help.
my siteGenius is 1% inspiration and 99% perspiration
Advertisement
If you want a dynamic array, look into ArrayList [or the 2.0 version, List]. If you look at the documentation for Array.Clear, you'll see that it is performing precicely as promised.

CM
The function Array.Clear( array, start, length ) sets all the elements in the range from start to start + length to 0 (or null if you are using object types). It does not remove them.

If you want to remove an element from an array I suggest you reverse the sorting order and resize the array to length -1. This way the last item will be deleted from the array (since it is sorted backwards, this will be the smallest number).

The problem is that resizing an array is a costly operation. It would be faster to use a List or a similar Collection item. These types have functions for deletion of items (also in the middle of a list), and are faster in these actions than an array.

Hope this helps.

Bert
Arrays really suck at this, you should be using ArrayList for something like this.

//Using ArrayListArrayList<int> intArray = new ArrayList<int>(10);intArray.RemoveAt(0);//Using a raw arrayInt32[] intArray = new Int32[10];Array.Reverse(intArray);Array.Resize(intArray, intArray.Length-1);//Same thing, but avoids the cost of resizing. (Yes I know, it's a hack)int intArraySize = 10;Int32[] intArray = new Int32[intArraySize];Array.Reverse(intArray);intArraySize--; //pretend that you removed the last item
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
Quote:Original post by deathkrush
Arrays really suck at this, you should be using ArrayList for something like this.

*** Source Snippet Removed ***


Wouldn't that be a List if you're using the generic List<int>?

As in,
List<int> intArray = new List<int>(10);intArray.Sort();intArray.RemoveAt(0);

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Quote:Original post by benryves
Quote:Original post by deathkrush
Arrays really suck at this, you should be using ArrayList for something like this.

*** Source Snippet Removed ***


Wouldn't that be a List if you're using the generic List<int>?

As in,
List<int> intArray = new List<int>(10);intArray.Sort();intArray.RemoveAt(0);


You are probably right. I can never remember which one is generic: Array, List, ArrayList, ListArray, ListMaybeArray...
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)

This topic is closed to new replies.

Advertisement