Results 1 to 3 of 3

Thread: Inserting new elements into the middle of an array...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    London UK
    Posts
    255

    Inserting new elements into the middle of an array...

    I have an array, and for every element beginning with "[@]" I need to stick another empty element after it. How could I do this?
    Not at all related to sheep...

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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());
    	}
    }

  3. #3
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    I probably would use and array list. Here is an example out of the documentation.

    PHP 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.Insert0"The" );
          
    myAL.Insert1"fox" );
          
    myAL.Insert2"jumped" );
          
    myAL.Insert3"over" );
          
    myAL.Insert4"the" );
          
    myAL.Insert5"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:" );
          
    PrintValuesmyAL );
          
    Console.WriteLine"The Queue initially contains the following:" );
          
    PrintValuesmyQueue );

          
    // Copies the Queue elements to the ArrayList at index 1.
          
    myAL.InsertRange1myQueue );

          
    // Displays the ArrayList.
          
    Console.WriteLine"After adding the Queue, the ArrayList now contains:" );
          
    PrintValuesmyAL );

          
    // Search for "dog" and add "lazy" before it.
          
    myAL.InsertmyAL.IndexOf"dog" ), "lazy" );

          
    // Displays the ArrayList.
          
    Console.WriteLine"After adding \"lazy\", the ArrayList now contains:" );
          
    PrintValuesmyAL );

          
    // Add "!!!" at the end.
          
    myAL.InsertmyAL.Count"!!!" );

          
    // Displays the ArrayList.
          
    Console.WriteLine"After adding \"!!!\", the ArrayList now contains:" );
          
    PrintValuesmyAL );

          
    // Inserting an element beyond Count throws an exception.
          
    try  {
             
    myAL.InsertmyAL.Count+1"anystring" );
          } catch ( 
    Exception myException )  {
             
    Console.WriteLine("Exception: " myException.ToString());
          }
       }

       public static 
    void PrintValuesIEnumerable 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()
    */ 
    Jeremy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width