-
November 11th, 2008, 01:47 PM
#1
string array - how to remove an item?
I'm a beginner at C#. I have a simple string array set up where keys is set as:
string[] keys = null;
I then have a for loop with a conditional phrase. If the element meets the condition, I want to remove the element. For a one dimensional array, I was hoping to be able to use:
keys[i].Remove(i,1);
However it doesn't seem to do anything so that all elements are still included in the array. Is there a better/easier way of doing this?
Help!
-
November 11th, 2008, 01:55 PM
#2
Re: string array - how to remove an item?
Use a List, nut an Array!!!!
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 11th, 2008, 02:02 PM
#3
Re: string array - how to remove an item?
Well the thing is, I'm taking over code for someone who already wrote everything. I just need to make some updates, and I'd like to do it quickly. If I do keep the code the way it is, is there a way to make it work?
-
November 11th, 2008, 02:13 PM
#4
Re: string array - how to remove an item?
There is no "good" way to make it sork, except for replacing the declaration.
You would have to actually copy all of the elements you wanted into either:
1) The same array (ie move down) and then track how many elements are "Active"
2) A new array of a smaller size.
For dynamicly sized (ie things you insert/delete to/from) you really want to use the collection classes.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 11th, 2008, 02:15 PM
#5
Re: string array - how to remove an item?
Code:
Array.Clear(keys, i, 1);
the Clear method accepts 3 parameters, the first being the array, second being the starting index, and the third being the number of elements to be deleted from the starting index.
EDITED: However, this doesn't seem to actually remove the element. It seems to basically set the element to an empty string. So this probably won't work for your situation.
Last edited by eclipsed4utoo; November 11th, 2008 at 02:21 PM.
-
November 11th, 2008, 02:20 PM
#6
Re: string array - how to remove an item?
Originally Posted by eclipsed4utoo
Code:
Array.Clear(keys, i, 1);
the Clear method accepts 3 parameters, the first being the array, second being the starting index, and the third being the number of elements to be deleted from the starting index
Clear does not delete/remove....
Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.
Code:
int [] a = { 1,2,3,4,5,6 }
a.Clear(a, 2, 3)
a == { 1,2,0,0,0, 6 } // NOT: a == {1,2,6}
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 11th, 2008, 02:40 PM
#7
Re: string array - how to remove an item?
is clear only a method of string arrays? when I use intellisense, I don't see a clear option.
-
November 11th, 2008, 02:47 PM
#8
Re: string array - how to remove an item?
Originally Posted by Bugz
is clear only a method of string arrays? when I use intellisense, I don't see a clear option.
Actually is it a static method....
Code:
Array.Clear(a,2,3);
Sorry for the typo....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 11th, 2008, 02:55 PM
#9
Re: string array - how to remove an item?
If you just refactor a bit and change the array to a list, you can then change the areas of your code that take that array as a parameter to use the List's .ToArray() method. It would probably only take a few minutes.
-
November 11th, 2008, 03:09 PM
#10
Re: string array - how to remove an item?
Originally Posted by BigEd781
If you just refactor a bit and change the array to a list, you can then change the areas of your code that take that array as a parameter to use the List's .ToArray() method. It would probably only take a few minutes.
Just ve very carefuly if the element type is a value type. ToArray() will return a COPY and changes to the COPY will NOT be reflected in the ORIGINAL.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 11th, 2008, 03:39 PM
#11
Re: string array - how to remove an item?
-
November 11th, 2008, 04:09 PM
#12
Re: string array - how to remove an item?
I like the way the clear method worked. Once it set my elements to null, I added in code to later ignore the null elements. Thank you so much for your help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|