ArrayList temp是一个已经被装入了数据的数组,怎么在最前面加入某一元素使虽有元素后移一位?
 
最笨的方法是转存成string[]用FOR循环使所有元素后移一位  但这也麻烦了点吧  有什么快捷的办法吗? 

解决方案 »

  1.   

    MSDN中 ArrayList.Insert() 方法 下面的代码示例演示如何向 ArrayList 中插入元素。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, "jumps" );
          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 )  {
          foreach ( Object obj in myList )
             Console.Write( "   {0}", obj );
          Console.WriteLine();
       }}