让我们来看看DotNet中System.Collections名字空间包含的可变数组对象.
1)ArrayList(数组列表)
本质上ArrayList对象就是一个可变长的数组,可以根据需要添加元素.使用ArrayList的方法可以向数组列表中添加元素,或取出,修改某个元素.
.Add()方法
using System;
using System.Collections;
class TestArrayList
{
static void Main()
{
ArrayList theArrayList =
new ArrayList();
theArrayList.Add("1");
theArrayList.Add("2");
foreach(string s in theArrayList)
{
Console.WriteLine(s);
}
}
}
using System.Collections;
class TestArrayList
{
static void Main()
{
ArrayList theArrayList =
new ArrayList();
theArrayList.Add("1");
theArrayList.Add("2");
foreach(string s in theArrayList)
{
Console.WriteLine(s);
}
}
}
.Clear 删除ArrayList中的内容
using System;
using System.Collections;
class TestArrayList
{
static void Main()
{
ArrayList theArrayList =
new ArrayList();
theArrayList.Add("1");
theArrayList.Add("2");
foreach(string s in theArrayList)
{
Console.WriteLine(s);
}
theArrayList.Clear();
Console.WriteLine(theArrayList.Count);
Console.Read();
}
}
using System.Collections;
class TestArrayList
{
static void Main()
{
ArrayList theArrayList =
new ArrayList();
theArrayList.Add("1");
theArrayList.Add("2");
foreach(string s in theArrayList)
{
Console.WriteLine(s);
}
theArrayList.Clear();
Console.WriteLine(theArrayList.Count);
Console.Read();
}
}
ArrayList theArrayList = new ArrayList();
theArrayList.Add("1");
theArrayList.Add("2");
string s = (string)theArrayList[0];
string s1 = (string)theArrayList[1];
theArrayList.Add("1");
theArrayList.Add("2");
string s = (string)theArrayList[0];
string s1 = (string)theArrayList[1];
using System;
using System.Collections;
class TestArrayList
{
static void Main()
{
ArrayList theArrayList =
new ArrayList();
theArrayList.Add("1");
theArrayList.Add("2");
foreach(string s in theArrayList)
{
Console.WriteLine(s);
}
if (theArrayList.Contains("1")) //判断字符中"1"是否存在于ArrayList中
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("NO");
}
Console.Read();
}
}
using System.Collections;
class TestArrayList
{
static void Main()
{
ArrayList theArrayList =
new ArrayList();
theArrayList.Add("1");
theArrayList.Add("2");
foreach(string s in theArrayList)
{
Console.WriteLine(s);
}
if (theArrayList.Contains("1")) //判断字符中"1"是否存在于ArrayList中
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("NO");
}
Console.Read();
}
}
CopyTo() 将ArrayList 全部内容拷贝到一个一维数组中
using System;
using System.Collections;
class TestArrayList
{
static void Main()
{
ArrayList theArrayList =
new ArrayList();
theArrayList.Add("1");
theArrayList.Add("2");
string [] a = new string[2];
theArrayList.CopyTo(a);
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
Console.Read();
}
}
using System.Collections;
class TestArrayList
{
static void Main()
{
ArrayList theArrayList =
new ArrayList();
theArrayList.Add("1");
theArrayList.Add("2");
string [] a = new string[2];
theArrayList.CopyTo(a);
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
Console.Read();
}
}
IndexOf()
using System;
using System.Collections;
class TestArrayList
{
static void Main()
{
ArrayList theArrayList =
new ArrayList();
string str1 = "1";
string str2 = "2";
int num1 = 1;
int num2 = 2;
theArrayList.Add(str1);
theArrayList.Add(str2);
theArrayList.Add(num1);
theArrayList.Add(num2);
Console.WriteLine(theArrayList.IndexOf(num1)); //返回改对象在ArrayList中的索引值
Console.WriteLine(theArrayList.IndexOf(str1));
Console.Read();
}
}
using System.Collections;
class TestArrayList
{
static void Main()
{
ArrayList theArrayList =
new ArrayList();
string str1 = "1";
string str2 = "2";
int num1 = 1;
int num2 = 2;
theArrayList.Add(str1);
theArrayList.Add(str2);
theArrayList.Add(num1);
theArrayList.Add(num2);
Console.WriteLine(theArrayList.IndexOf(num1)); //返回改对象在ArrayList中的索引值
Console.WriteLine(theArrayList.IndexOf(str1));
Console.Read();
}
}