Sign in to follow this  
silverphyre673

[.net] Removing elements from an array

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 this post


Link to post
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 this post


Link to post
Share on other sites
Arrays really suck at this, you should be using ArrayList for something like this.


//Using ArrayList
ArrayList<int> intArray = new ArrayList<int>(10);
intArray.RemoveAt(0);

//Using a raw array
Int32[] 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 this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this