C#入门经典之动态数组ArrayList常用的方法讲解
Vivian 2018-05-22 来源 : 阅读 1225 评论 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频道!

本文由 @Vivian 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 1
看完这篇文章有何感觉?已经有1人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程