Vivian
2018-05-22
来源 :
阅读 1699
评论 0
摘要:C#入门经典中,动态的增加和减少元素实现了ICollection和List和IEnumerable接口灵活的设置数组大小不安全的集合类型其元素为值类型时效率不高(装箱和拆箱导致效率不高),希望对大家学习C#入门经典有所帮助。
动态数组
动态的增加和减少元素实现了ICollection和List和IEnumerable接口灵活的设置数组大小不安全的集合类型其元素为值类型时效率不高(装箱和拆箱导致效率不高),希望对大家学习C#入门经典有所帮助。
ArrayList常用的方法
//to create an ArrayList
ArrayList arrayList = new ArrayList();
//to add values
arrayList.Add(32);
arrayList.Add("Puma");
arrayList.Add('a');
//ArrayList can have different kinds of type of value
//to output
foreach (object obj in arrayList)
{
Console.Write(obj + " ");
}
Console.WriteLine();
//show the capacity
Console.WriteLine("the Capacity is:" + arrayList.Capacity);
Console.WriteLine("Adding one more element");
arrayList.Add(5);
Console.WriteLine("Now the Capacity is:" + arrayList.Capacity);
//count: gets the number of elements actually contained in ArrayList
Console.WriteLine("The elements in the ArrayList are:" + arrayList.Count);
//contains : Determines whether an element is in the ArrayList
if(arrayList.Contains(32))
Console.WriteLine("It exists!");
else
Console.WriteLine("It doesn'n exist");
//Insert: Insert an element into ArrayList at the specified index
arrayList.Insert(2,"real");
foreach (object obj in arrayList)
{
Console.Write(obj + " ");
}
Console.WriteLine();
//IndexOf:searches for the specified object and returns the zero-based index of the first occurrence within the entire ArrayList.
Console.WriteLine(arrayList.IndexOf(32));
//Remove:Removes the first occurrence of a specific object from the ArrayList
arrayList.Remove(32);
foreach (object obj in arrayList)
{
Console.Write(obj + " ");
}
Console.WriteLine();
//Reverse:Reverses the order of the elements in the entire ArrayList
arrayList.Reverse();
foreach (object obj in arrayList)
{
Console.Write(obj + " ");
}
Console.WriteLine();
//Sort:Sorts the elements in the entire ArrayList.
arrayList.Add(3);
arrayList.Add(42);
arrayList.Remove("Puma");
arrayList.Remove("real");
arrayList.Remove('a');
//before sorting
foreach (object obj in arrayList)
{
Console.Write(obj + " ");
}
Console.WriteLine();
arrayList.Sort();
//after sorting
foreach (object obj in arrayList)
{
Console.Write(obj + " ");
}
Console.WriteLine();本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!
喜欢 | 0
不喜欢 | 1
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号