ArrayList数组中 怎样在最前面加一个元素? ArrayList temp是一个已经被装入了数据的数组,怎么在最前面加入某一元素使虽有元素后移一位? 最笨的方法是转存成string[]用FOR循环使所有元素后移一位 但这也麻烦了点吧 有什么快捷的办法吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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(); }} 如何通过实际行数取出字段id(主键且自增)的值 帮帮我啊..服务器相关问题 sql 删除数据问题 求助:关于查询的问题: 用SELECT查询时出了问题怎么搞?? 急!~.net高手进 请问怎么做带图片的ComboBox呀? 大家帮我看看,数据库的问题?? WebForm 中CheckBox中的用法:当一个CheckBox点击后,其他几个CheckBox自动设为true. 请问大家一个邮件的问题,急,分不够再加 再问:关于Hook的问题 如何实现拖动打开文件 请问C#.net用什么方法连接excel文件,然后实行查询操作,将结果显示到form中。
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();
}}