震惊!大学都快毕业了,某男子居然还不知道ArrayList集合是什么
2020-06-09 04:56
369 查看
文章目录
列表集合 ArrayList
ArrayList集合的使用
ArrayList非常类似于数组,因为其容量可按照需要动态调整,且也是通过下标访问元素,
也称之为动态数组。
ArrayList集合常用的属性中存在两个重要属性:Capacity和Count。其中Capacity用于获取集合的容量,Count用于获取集合中存储元素的个数。
与使用数组不同,创建 ArrayList对象时,不需要指定容量,其容量将会动态调整。
下面通过一个案例演示一下ArrayList集合的容量变化规则。
新建一个Customer类,代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LieBiaoJiHe { public class Customer { //定义一个无参的构造方法 public Customer() { } //定义一个有参数的构造方法 public Customer(string name, int age, string address) { Name = name; Age = age; Address = address; } public string Name { get; set; } public int Age { get; set; } public string Address { get; set; } } }
主方法中代码如下:当我们设置 ArrayList的容量为2时,如果容量超出,结果会怎样?
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LieBiaoJiHe { class Program { static void Main(string[] args) { //创建两个Customer对象并实例化 Customer customer_1 = new Customer("张三", 18, "北京"); Customer customer_2 = new Customer("李四", 19, "上海"); Customer customer_3 = new Customer("王五", 20, "深圳"); //创建一个集合对象 ArrayList list = new ArrayList(2);//设置容量为2 Console.WriteLine("**********"); Console.WriteLine("集合容量:"+list.Capacity); Console.WriteLine("数据个数:"+list.Count); list.Add(customer_1); Console.WriteLine("**********"); Console.WriteLine("集合容量:" + list.Capacity); Console.WriteLine("数据个数:" + list.Count); list.Add(customer_2); Console.WriteLine("**********"); Console.WriteLine("集合容量:" + list.Capacity); Console.WriteLine("数据个数:" + list.Count); list.Add(customer_3); Console.WriteLine("**********"); Console.WriteLine("集合容量:" + list.Capacity); Console.WriteLine("数据个数:" + list.Count); } } }
运行结果如图所示:
我们手动设置了ArrayList集合的为2后,当集合中的对象有3个时,超出集合容量,此时ArrayList集合的容量会自动扩大一倍。以此类推,当集合中的元素有5个时,集合的容量就变成8了。由此我们可以得出一个规律:ArrayList中存储的元素超出容量时,其容量将自动增长一倍。
ArrayList集合常用的方法
第一类:用于添加元素的方法:Add、Insert
第二类:用于删除元素的方法:Remove、RomoveAt、Clear
第三类:排序和反转的方法:Sort、Reverse
方法 | 作用 |
---|---|
Add | 将元素添加到ArrayList结尾处 |
Insert | 将元素添加到ArrayList的指定索引处 |
Remove | 通过对象删除,若对象存在则删除成功;否则,删除失败 |
RomoveAt | 删除指定索引处的元素 |
Clear | 将集合中的所有元素清除 |
Sort | 将集合中的元素进行排序 |
Reverse | 将集合中的元素顺序进行反转 |
添加元素方法的使用:
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LieBiaoJiHe { class Program { static void Main(string[] args) { //创建两个Customer对象并实例化 Customer customer_1 = new Customer("张三", 18, "北京"); Customer customer_2 = new Customer("李四", 19, "上海"); Customer customer_3 = new Customer("王五", 20, "深圳"); //创建一个集合对象 ArrayList list = new ArrayList(); //添加元素 list.Add(customer_1); //Add,添加到ArrayList结尾处 list.Add(customer_2); //此时ArrayList里面存储元素的顺序为:0:customer_1,1:customer_2 list.Insert(1, customer_3); //Insert,添加到ArrayList的指定索引处 //此时ArrayList里面存储元素的顺序为:0:customer_1,1:customer_3,2:customer_2 //ArrayList和数组一样,通过下标对元素进行访问,下标从0开始,且需要进行类型转换。 //获取数据 /*方法1,不建议使用,代码太繁琐 Customer temp = list[0] as Customer; //将list转换为Customer类型 Console.WriteLine("客户姓名:" + temp.Name); temp = list[1] as Customer; Console.WriteLine("客户姓名:" + temp.Name); temp = list[2] as Customer; Console.WriteLine("客户姓名:" + temp.Name); */ //方法2,使用for循环获取数据 Console.WriteLine("使用for循环获取数据"); for (int i = 0; i < list.Count; i++) { Customer temp = list[i] as Customer; //将list转换为Customer类型 Console.WriteLine("客户姓名:"+temp.Name); } Console.WriteLine("*****************"); //方法3,使用foreach循环获取数据 Console.WriteLine("使用foreach循环获取数据"); foreach (object item in list) { Customer temp = item as Customer;//类型转换,将item转换为Customer类型 Console.WriteLine("客户姓名:" + temp.Name); } } } }
删除元素方法的使用:
在ArrayList中,删除靠前的元素,之后的元素将自动向前移动。
namespace LieBiaoJiHe { class Program { static void Main(string[] args) { //创建两个Customer对象并实例化 Customer customer_1 = new Customer("张三", 18, "北京"); Customer customer_2 = new Customer("李四", 19, "上海"); Customer customer_3 = new Customer("王五", 20, "深圳"); //创建一个集合对象 ArrayList list = new ArrayList(); //添加元素 list.Add(customer_1); //Add,添加到ArrayList结尾处 list.Add(customer_2); list.Insert(1, customer_3); //删除元素 list.Remove(customer_1); Customer customer_4 = new Customer("王五", 20, "深圳"); list.Remove(customer_4);//删除失败,customer_4没有添加到集合中来 //此时集合中元素为 0:customer_3, 1:customer_2 list.RemoveAt(1);//删除索引位置在1的元素 //此时集合中元素为 0:customer_3 list.Clear(); //清除集合中的所有元素 //使用for循环获取数据 Console.WriteLine("使用for循环获取数据"); for (int i = 0; i < list.Count; i++) { Customer temp = list[i] as Customer; //将list转换为Customer类型 Console.WriteLine("客户姓名:"+temp.Name); } Console.WriteLine("*****************"); //使用foreach循环获取数据 Console.WriteLine("使用foreach循环获取数据"); foreach (object item in list) { Customer temp = item as Customer;//类型转换,将item转换为Customer类型 Console.WriteLine("客户姓名:" + temp.Name); } } } }
排序方法的使用:
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LieBiaoJiHe { class Program { static void Main(string[] args) { //创建两个Customer对象并实例化 Customer customer_1 = new Customer("张三", 18, "北京"); Customer customer_2 = new Customer("李四", 19, "上海"); Customer customer_3 = new Customer("王五", 20, "深圳"); Customer customer_4 = new Customer("赵六", 16, "广州"); //创建一个集合对象 ArrayList list = new ArrayList(); //添加元素 list.Add(customer_1.Age); list.Add(customer_2.Age); list.Add(customer_3.Age); list.Add(customer_4.Age); list.Sort();//默认按照升序进行排列 list.Reverse();//反转集合中的元素,此时排序方式由原来的升序变成了降序 foreach (var item in list) { Console.WriteLine(item); } } } }
使用Sort方法排序后的结果如图所示:Sort方法默认为升序进行排列
使用Reverse方法进行反转后的结果如图所示:由原来的升序变成了降序
相关文章推荐
- 大学刚毕业做测试工作 感觉不知道要干什么
- 毕业快两年了,也不知道在做什么,回忆一下过去.---ZT
- 河北男子大学毕业求职碰壁 回炉上中职找到工作
- 大学毕业了,我会什么?
- 从22岁大学毕业在26岁之间这四年,重要的不是你做了什么,重要的是你在工作中养成了怎么样的良好的工作习惯
- 大学毕业究竟看什么?
- 大学毕业意味着什么?进修出来意味着什么?
- 毕业了不知道干什么
- 1.一男子在路边一根接着一根地抽烟。一个女士走过来对他说:“嘿,你不知道你是在慢性自杀吗?注意看看烟盒上的警告信息。”“没关系”, 男子悠然自得地又吸了一口:“我是个程序员。”“嗯?这和你是程序员有什么关系?...
- 大学毕业刚转行做计算机,毫无头绪,不知道怎么规划自己的技术?
- 纠结了很久,不知道为什么除了什么原因,用任何方查都查不到根源,后来ArrayList存放了Bitmap,结果出错了,终于找到了事情的根源,一阵无语,不能存放竟静态的,为什么不提示呢, 害我以为是那些自
- 555~~没考好。。真不知道自己能上个什么大学。。
- 国内大学毕业论文LaTeX模板集合
- 国内大学毕业论文LaTeX模板集合
- 国内大学毕业论文LaTeX模板集合
- ArrayList集合中remove()方法分析(根据什么remove()删除)
- 国内大学毕业论文LaTeX模板集合
- 马云马化腾等IT大佬都毕业于什么大学
- 大学刚毕业找什么类型的公司好?成熟的大公司或者创业型的公司 推荐
- 国内大学毕业论文LaTeX模板集合