I have an array, and for every element beginning with "[@]" I need to stick another empty element after it. How could I do this?
Printable View
I have an array, and for every element beginning with "[@]" I need to stick another empty element after it. How could I do this?
There probably is easier ways, but here is something I whipped up for you.
Code:private void button1_Click(object sender, System.EventArgs e)
{
string[] x;
string[] y;
int counter;
int interiorCounter;
counter = 0;
interiorCounter = 0;
// X is our original array.
x = new string[5];
x[0] = "hello";
x[1] = "@";
x[2] = "something";
x[3] = "@";
x[4] = "@";
// We need to find out how many elements we are going to be adding.
for (int i = 0; i <= x.GetUpperBound(0); i++)
{
if (x[i] == "@")
counter++;
}
// Now we know how many to add, we need to add the x
// array amount to the counter variable for the total.
counter += x.GetUpperBound(0) + 1;
// Now instantiate our new array.
y = new string[counter];
// Start filling the new array.
for (int i = 0; i <= x.GetUpperBound(0); i++)
{
if(x[i] == "@")
{
y[interiorCounter] = "@";
interiorCounter++;
y[interiorCounter] = "your newly added element";
}
else
{
y[interiorCounter] = x[i];
}
interiorCounter++;
}
// Show your newly added stuff....
for(int i = 0; i <= y.GetUpperBound(0); i++)
{
MessageBox.Show(y[i].ToString());
}
}
I probably would use and array list. Here is an example out of the documentation.
JeremyPHP Code:using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes a new ArrayList using Insert instead of Add.
ArrayList myAL = new ArrayList();
myAL.Insert( 0, "The" );
myAL.Insert( 1, "fox" );
myAL.Insert( 2, "jumped" );
myAL.Insert( 3, "over" );
myAL.Insert( 4, "the" );
myAL.Insert( 5, "dog" );
// Creates and initializes a new Queue.
Queue myQueue = new Queue();
myQueue.Enqueue( "quick" );
myQueue.Enqueue( "brown" );
// Displays the ArrayList and the Queue.
Console.WriteLine( "The ArrayList initially contains the following:" );
PrintValues( myAL );
Console.WriteLine( "The Queue initially contains the following:" );
PrintValues( myQueue );
// Copies the Queue elements to the ArrayList at index 1.
myAL.InsertRange( 1, myQueue );
// Displays the ArrayList.
Console.WriteLine( "After adding the Queue, the ArrayList now contains:" );
PrintValues( myAL );
// Search for "dog" and add "lazy" before it.
myAL.Insert( myAL.IndexOf( "dog" ), "lazy" );
// Displays the ArrayList.
Console.WriteLine( "After adding \"lazy\", the ArrayList now contains:" );
PrintValues( myAL );
// Add "!!!" at the end.
myAL.Insert( myAL.Count, "!!!" );
// Displays the ArrayList.
Console.WriteLine( "After adding \"!!!\", the ArrayList now contains:" );
PrintValues( myAL );
// Inserting an element beyond Count throws an exception.
try {
myAL.Insert( myAL.Count+1, "anystring" );
} catch ( Exception myException ) {
Console.WriteLine("Exception: " + myException.ToString());
}
}
public static void PrintValues( IEnumerable myList ) {
System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "\t{0}", myEnumerator.Current );
Console.WriteLine();
}
}
/*
This code produces the following output.
The ArrayList initially contains the following:
The fox jumped over the dog
The Queue initially contains the following:
quick brown
After adding the Queue, the ArrayList now contains:
The quick brown fox jumped over the dog
After adding "lazy", the ArrayList now contains:
The quick brown fox jumped over the lazy dog
After adding "!!!", the ArrayList now contains:
The quick brown fox jumped over the lazy dog !!!
Exception: System.ArgumentOutOfRangeException: Insertion index was out of range. Must be non-negative and less than or equal to size.
Parameter name: index
at System.Collections.ArrayList.Insert(Int32 index, Object value)
at SamplesArrayList.Main()
*/