Repeat方法返回一个ArrayList,其元素是指定值的副本。
这意味着在此方法中,我们可以给出指定的值,然后将该值复制到ArrayList。
C# ArrayList中的Repeat方法示例
namespace ConsoleApplication3 { class Program { static void Main(string[] args) { //创建一个新的ArrayList //并初始化为null ArrayList myArray = ArrayList.Repeat(null, 10); //查看ArrayList的 count, capacity 属性以及值 Console.WriteLine("ArrayList with five elements with a null value"); Console.WriteLine(" Count : {0}", myArray.Count); Console.WriteLine(" Capacity : {0}", myArray.Capacity); Console.Write(" Values:"); foreach (Object obj in myArray) Console.Write(" {0}", obj); Console.WriteLine(); //创建一个新的ArrayList //每个元素都使用字符串Repeat进行初始化 myArray = ArrayList.Repeat("Repeat", 5); //查看ArrayList的 count, capacity 属性以及值 Console.WriteLine("ArrayList with seven elements with a string value"); Console.WriteLine(" Count : {0}", myArray.Count); Console.WriteLine(" Capacity : {0}", myArray.Capacity); Console.Write(" Values:"); foreach (Object obj in myArray) Console.Write(" {0}", obj); Console.WriteLine(); } } }
日期:2020-04-11 23:03:49 来源:oir作者:oir