以下是ArrayList里常用的方法,还有一个枚举哦。 项目都是这样一步一步累积起来的,多写写,赫赫。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace TestArayListClass
...{
class Program
...{
static void Main(string[] args)
...{
ArrayList testAL = new ArrayList();
for(int i = 0;i<10;i++)
...{
testAL.Add(i+1);
}
testAL.Add(8);
Program test = new Program();
test.PrintValues(testAL);
Console.WriteLine("");
Console.WriteLine("元素个数是"+testAL.Count);
Console.WriteLine("总容量是"+testAL.Capacity);
Console.WriteLine("下面进行元素查找");
test.FindObject(testAL, 3);
test.FindObject(testAL, 8);
Console.WriteLine("下面将'4'移除法");
testAL.Remove(4);
test.PrintValues(testAL);
Console.WriteLine("下面将下标为4的元素移除");
testAL.RemoveAt(4);
test.PrintValues(testAL);
Console.WriteLine("删除2-6范围的元素");
testAL.RemoveRange(2, 6);
test.PrintValues(testAL);
Console.WriteLine("将A-E的字母添加到当前对象");
string[] s = new string[] ...{ "A","B","C","D","E"};
testAL.AddRange(s);
test.PrintValues(testAL);
Console.WriteLine("下面执行清楚所有元素的操作");
testAL.Clear();
test.PrintValues(testAL);
Console.WriteLine("当前元素的个数是:"+testAL.Count);
Console.WriteLine("当前元素的总量是:"+testAL.Capacity);
Console.ReadLine();
}
public void PrintValues(IEnumerable testList)
...{
System.Collections.IEnumerator testEnumerator = testList.GetEnumerator();
Console.Write("当前所有元素的值:");
while (testEnumerator.MoveNext())
Console.Write("" + testEnumerator.Current);
}
public void FindObject(ArrayList testList,Object obj)
...{
int index = testList.BinarySearch(obj);
if (index > 0)
Console.WriteLine(" 值 为 " + obj.ToString() + "的对象是第" + index + "元素");
else
Console.WriteLine("当前对象包括该值。");
}
}
}
ArrayList操作指南
本文通过一个示例程序展示了如何使用C#中的ArrayList类。包括添加、查找、移除元素及清空列表等基本操作。
675

被折叠的 条评论
为什么被折叠?



