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)