ArrayList使用(下)

                                                ArrayList使用(下)

1.删除ArrayList集合元素
1.1 Clear方法
int[] arr=new int[]{1,2,3,4,5,6};
ArrayList List=new ArrayList(arr);
List.Clear();
1.2 Remove方法
int[] arr=new int[]{1,2,3,4,5,6};
ArrayList List=new ArrayList(arr);
List.Remove(3);
1.3 RemoveAt方法
int[] arr=new int[]{1,2,3,4,5,6};
ArrayList List=new ArrayList(arr);
List.RemoveAt(3);
1.4 RemoveRange方法
移除一定范围的元素
int[] arr=new int[]{1,2,3,4,5,6};
ArrayList List=new ArrayList(arr);
List.RemoveRange(3,2);
例子:
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ArrayList List = new ArrayList(arr);
Console.WriteLine(“原ArrayList的集合:”);
foreach(int i in List)
{
Console.Write(i.ToString() + " ");
}
Console.WriteLine();
List.RemoveRange(1, 4);
Console.WriteLine(“移除后ArrayList集合:”);
foreach(int i in List)
{
Console.Write(i.ToString() + " ");
}
Console.ReadLine();
]

2.遍历ArrayList集合
static void Main(string[] args)
{
ArrayList List = new ArrayList();
List.Add(“C#编程”);
List.Add(“C#实战”);
foreach(string str in List)
{
Console.WriteLine(str);
}
Console.ReadLine();
}
3.查找ArrayList集合元素
3.1 Contains方法
int[] arr=new int[]{1,2,3,4,5,6};
ArrayList List=new ArrayList(arr);
Console.Write(List.Contains(2)); //判断ArrayList集合中是否包含指定的元素
3.2 IndexOf方法
int[] arr=new int[]{1,2,3,4,5,6};
ArrayList List=new ArrayList(arr);
Console.Write(List.IndexOf(2)); //顺序查找数字2的索引位置
(1)
3.3 LastIndexOf方法
int[] arr=new int[]{1,2,3,4,5,6};
ArrayList List=new ArrayList(arr);
Console.Write(List.LastIndexOf(2)); //倒序查找数字2的索引位置
(4)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值