您的位置:首页 > 其它

震惊!大学都快毕业了,某男子居然还不知道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方法进行反转后的结果如图所示:由原来的升序变成了降序

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐