📄 sample30.cs
字号:
namespace apiBook
{
using System;
using System.Collections;
public class TestArrayListClass
{
public static void Main()
{
ArrayList testAL = new ArrayList();
//创建一个ArrayList对象
for(int i=0;i<10;i++)
{
testAL.Add(i+1);
}
testAL.Add(8);
//使用Add方法添加元素
TestArrayListClass test=new TestArrayListClass();
test.PrintValues(testAL);
//用来打印所有元素的自定义方法
Console.WriteLine( "元素个数是:"+testAL.Count);
Console.WriteLine( "总容量是:"+testAL.Capacity+"个");
Console.WriteLine("下面进行元素的查找");
test.FindObject(testAL,3);
//查找对象的方法,里面使用BinarySearch方法进行查找
test.FindObject(testAL,8);
Console.WriteLine("下面将'4'移除");
testAL.Remove(4);
//使用Remove方法移除对应元素
test.PrintValues(testAL);
Console.WriteLine("下面将下标为4的元素移除");
testAL.RemoveAt(4);
//使用RemoveAt方法移除第4个元素
test.PrintValues(testAL);
Console.WriteLine("删除2-6范围的元素:");
testAL.RemoveRange(2,6);
//使用RemoveRange方法来移除某范围的元素
test.PrintValues(testAL);
Console.WriteLine("将A-E的字母添加到当前对象");
String[] s=new String[]{"A","B","C","D","E"};
testAL.AddRange(s);
//使用AddRange方法一次添加多个元素
test.PrintValues(testAL);
Console.WriteLine("下面执行清除所有元素的操作");
testAL.Clear();
//使用Clear方法清除所有元素
test.PrintValues(testAL);
Console.WriteLine("当前ArrayList对象的元素个数是:"+testAL.Count);
Console.WriteLine("当前ArrayList对象的总容量是:"+testAL.Capacity);
Console.ReadLine();
}
public void FindObject( ArrayList testList, Object obj )
{
int index=testList.BinarySearch( obj );
//使用BinarySearch方法进行查找
if ( index > 0 )
Console.WriteLine( "值为"+obj.ToString()+"的对象是第"+index+"元素");
else
Console.WriteLine( "当前对象包括该值。");
}
public void PrintValues(IEnumerable testList )
{
System.Collections.IEnumerator testEnumerator = testList.GetEnumerator();
Console.Write("当前所有元素值:");
while ( testEnumerator.MoveNext() )
Console.Write( " "+testEnumerator.Current );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -