Answered by:
How to remove the first element in a string array?

Question
-
I want to remove the first element in a string array. My attempt to do so:
gsArrFirstFile[].Remove(0);
...backfires with a "Syntax error; value expected"
How is this done?
Photographer/Writer/Drummer: www.iceagetrail.posterous.comTuesday, May 18, 2010 7:16 PM
Answers
-
If you declared the string array using the base notation, that is something like:
String[] gsArrFirstFile = new String[100];
then I don't think you can easily change the dimension of the array because it's fixed. To demonstrate this, just invoke the following:
Console.WriteLine(gsArrFirstFile.IsFixedSize); //Returns true
As an alternative you can use the collections' wrappers .NET exposes, like the ArrayList, in the following manner:
ArrayList myArray = new ArrayList(); myArray.Add("Hello"); myArray.Add("See you"); myArray.Add("Good bye"); myArray.RemoveAt(0); foreach (Object item in myArray) { Console.WriteLine(item.ToString()); }
It prints the remaining two items after deleting the first item, in zero position.
Giuseppe
Tuesday, May 18, 2010 7:58 PM -
Arrays cannot have items removed from indices, to do that you basically move everything in the array up one and it makes space at the end. Use a List or a Queue depending on your needs and use RemoveAt.Tuesday, May 18, 2010 8:08 PM
-
<<Use a List or a Queue depending on your needs and use RemoveAt.>>
How about a StringBuilder?
It depends on your program, considering the performance of a concatenation, StringBuilder is better, as it doesn't always allocate new memory when appending new members.Thanks.
Figo Fei
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Wednesday, May 19, 2010 3:25 AM -
-
Use List<string> or Queue<string>, no need to work with objects and use an ArrayList or other dated objects. Best coding practice is to use a string generic. Afterwards if the performance isn't good enough we'll go from there.Wednesday, May 19, 2010 8:09 PM
All replies
-
gsArrFirstFile.Remove(gsArrFirstFile[0]);
WHAT'S NEW IN THE .NET FRAMEWORK 4:
Article: Comparison of parallel and sequential computing in .NET FrameworkTuesday, May 18, 2010 7:29 PM -
If you declared the string array using the base notation, that is something like:
String[] gsArrFirstFile = new String[100];
then I don't think you can easily change the dimension of the array because it's fixed. To demonstrate this, just invoke the following:
Console.WriteLine(gsArrFirstFile.IsFixedSize); //Returns true
As an alternative you can use the collections' wrappers .NET exposes, like the ArrayList, in the following manner:
ArrayList myArray = new ArrayList(); myArray.Add("Hello"); myArray.Add("See you"); myArray.Add("Good bye"); myArray.RemoveAt(0); foreach (Object item in myArray) { Console.WriteLine(item.ToString()); }
It prints the remaining two items after deleting the first item, in zero position.
Giuseppe
Tuesday, May 18, 2010 7:58 PM -
Arrays cannot have items removed from indices, to do that you basically move everything in the array up one and it makes space at the end. Use a List or a Queue depending on your needs and use RemoveAt.Tuesday, May 18, 2010 8:08 PM
-
<<Use a List or a Queue depending on your needs and use RemoveAt.>>
How about a StringBuilder?
Photographer/Writer/Drummer: www.iceagetrail.posterous.comTuesday, May 18, 2010 8:48 PM -
<<Use a List or a Queue depending on your needs and use RemoveAt.>>
How about a StringBuilder?
It depends on your program, considering the performance of a concatenation, StringBuilder is better, as it doesn't always allocate new memory when appending new members.Thanks.
Figo Fei
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Wednesday, May 19, 2010 3:25 AM -
-
You should also look at Stack as well.
1. How to Create a Stack?
Stack s = new Stack(32);
32 is the intial size of the Stack. It is always better to give intially size so that the Push/Pop operations are performed efficiently with out re-arranging the elements.
2. To Push a element to the Stack?s.Push(12);
Push will place an element on the top of stack and also the element which is to be removed first.
3. How to Pop an Element?object ob = s.Pop()
My .NET Blog: http://michaelcrump.netWednesday, May 19, 2010 4:21 PM -
I just want to point out that the reason B CLay got a synthax error is because he didn't specify the index of his array. In other words this:
gsArrFirstFile[0].Remove(0);
should have compiled but would have been useless.
Now this,
gsArrFirstFile[0] = gsArrFirstFile[0].Remove(0);
would have cleared the contents of the first element but would not have removed the element itself from the array.
gsArrFirstFile[0] =null;
Would have worked just as well.
So, as others have pointed out, String arrays do not possess the ability to remove elements from themselves. If this is needed and simply checking if a particular element is null won't do, I recomment either using an ArrayList of strings along with its RemoveAt method or a LinkedList (which would work better if you have a large list that you have to deal with).
Wednesday, May 19, 2010 6:19 PM -
Use List<string> or Queue<string>, no need to work with objects and use an ArrayList or other dated objects. Best coding practice is to use a string generic. Afterwards if the performance isn't good enough we'll go from there.Wednesday, May 19, 2010 8:09 PM
-
It's unreasonable to completely dismiss ArrayList objects as dated. While it certainly comes at a cost performancewise, this data type provides functionality that is often very useful. Just as there are a lot of times when Queue<string> simply wouldn't do what's needed despite the fact that it is more efficient. It all comes down to what is actually required and I simply don't know enough to make a cal in relation to this project.
And I say that generally LinkedList data type is preferrable when dealing with large lists though their functionality is slightly more limited. For realtive small lists however, there really isn't much difference in terms of resources so it's really negligible.
The real best coding practice calls for using the most appropriate type for what's needed. Even by using this criteria ArrayLists have their place.
Thursday, May 20, 2010 1:01 AM -
public static T[] Remove<T>(this T[] array, int index) { Array.Copy(array, index + 1, array, index, array.Length - index - 1); Array.Resize(ref array, array.Length - 1); return array; }
Thursday, May 20, 2010 1:04 PM -
It's unreasonable to completely dismiss ArrayList objects as dated. While it certainly comes at a cost performancewise, this data type provides functionality that is often very useful. Just as there are a lot of times when Queue<string> simply wouldn't do what's needed despite the fact that it is more efficient. It all comes down to what is actually required and I simply don't know enough to make a cal in relation to this project.
And I say that generally LinkedList data type is preferrable when dealing with large lists though their functionality is slightly more limited. For realtive small lists however, there really isn't much difference in terms of resources so it's really negligible.
The real best coding practice calls for using the most appropriate type for what's needed. Even by using this criteria ArrayLists have their place.
What functionality does ArrayList have that List<> doesn't? I see no reason to ever use ArrayList. List<> is more performant and type safe.[Edit] Unless you're using a version/have to be compatible with a version of .NET that doesn't have generics.
Thursday, May 20, 2010 2:35 PM