[.net] Removing elements from an array

This topic is 4278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
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

Share on other sites
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

Share on other sites
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

Share on other sites
Quote:
 Original post by deathkrushArrays 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);

Share on other sites
Quote:
Original post by benryves
Quote:
 Original post by deathkrushArrays 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...

• 17
• 11
• 12
• 9
• 49
• Forum Statistics

• Total Topics
631395
• Total Posts
2999758
×