版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
ArrayList集合
所有的集合都是通过数组实现的,只是对数组进行了封装。
//集合的长度不是固定的,另外集合可以存储各种不同数据类型的元素
ArrayList arrayList = new ArrayList();
arrayList.Add(1);
arrayList.Add("嘎嘎嘎");
arrayList.Add(true);
arrayList.AddRange(new string[] { "sas", "sasa", "saas" });
MessageBox.Show(arrayList[0].ToString());//集合也可以通过索引进行访问
MessageBox.Show(arrayList.Count.ToString());//集合的count属性返回集合的元素个数
MessageBox.Show(arrayList.Capacity.ToString());//集合的capacity属性返回的是集合的容量
arrayList.Insert(0,"张三");//向集合中指定位置插入单个元素
arrayList.InsertRange(1, new char[] { 'a', 'b', 'c' });//向集合中指定位置插入一个数组或者集合
arrayList.Remove("张三");//删除集合中指定的元素
arrayList.RemoveAt(2);//通过索引删除集合中的元素
arrayList.RemoveRange(2, 3);//删除集合中一定范围的元素
//如果想要清空集合中的所有元素,不要使用for循环遍历索引进行删除,因为集合的索引是随着元素的个数变化在变化的。要使用clear()方法进行清空。
//而且也不能使用foreach循环进行清空集合中的元素,因为foreach循环是只读操作,无法对其修改。
arrayList.Clear();//使用clear()方法清空集合中的所有元素
arrayList.Contains("张三");//contains()方法用来判断集合中是否包含某个元素
object[] o = arrayList.ToArray();//ToArray()方法将集合转成数组
实现IComparable接口,使这个类的实例能够进行排序
public class Student: IComparable//实现了IComparable接口,重写了CompareTo(0方法,实现了Student这个类实例的比较
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
int IComparable.CompareTo(object obj)
{
Student s = (Student)obj;
//return this.Age - s.Age;//按照年龄升序排序
//return s.Age - this.Age;//按照年龄降序排序
//return this.Name.Length - s.Name.Length;//按照名字的长度进行升序排序
return s.Name.Length - this.Name.Length;//按照名字的长度进行降序排列
}
}
Student s1 = new Student();
s1.Name = "张2三";
s1.Age = 20;
Student s2 = new Student();
s2.Name = "李22222四";
s2.Age = 25;
Student s3 = new Student();
s3.Name = "王33五";
s3.Age = 29;
Student s4 = new Student();
s4.Name = "赵2222222六";
s4.Age = 27;
ArrayList arrayList = new ArrayList();
arrayList.AddRange(new Student[] { s1, s2, s3, s4 });
arrayList.Sort();
for (int i = 0; i < arrayList.Count; i++)
{
MessageBox.Show(((Student)arrayList[i]).Name.ToString());
}
另一种实现自定义类进行排序的方法,写一个比较器的类
public class ComparerByNameLengthAsc : IComparer//实现了IComparer接口,这个类就是一个比较器
{
public int Compare(object x, object y)
{
Student s1 = x as Student;
Student s2 = y as Student;
return s1.Name.Length - s2.Name.Length;//按照名字的长度进行排序
}
}
Student s1 = new Student();
s1.Name = "张2三";
s1.Age = 20;
Student s2 = new Student();
s2.Name = "李22222四";
s2.Age = 25;
Student s3 = new Student();
s3.Name = "王33五";
s3.Age = 29;
Student s4 = new Student();
s4.Name = "赵2222222六";
s4.Age = 27;
ArrayList arrayList = new ArrayList();
arrayList.AddRange(new Student[] { s1, s2, s3, s4 });
arrayList.Sort(new ComparerByNameLengthAsc());//因为ComparerByNameLengthAsc类实现了IComparer接口,所以此处直接传一个ComparerByNameLengthAsc对象就可以
for (int i = 0; i < arrayList.Count; i++)
{
MessageBox.Show(((Student)arrayList[i]).Name.ToString());
}
HashTable集合
Hashtable ht = new Hashtable();
//键值对集合在添加元素的时候键一定不能重复
ht.Add(1, "张三");
ht.Add(2, new Student() { Name = "李四", Age = 20 });
MessageBox.Show(ht[1].ToString());
MessageBox.Show(((Student)ht[2]).Name);
bool b = ht.ContainsKey(1);//判断键值对集合中是否包含某个键
MessageBox.Show(b.ToString());
//遍历键值对的键
foreach (var item in ht.Keys)
{
MessageBox.Show(item.ToString());
}
//遍历键值对中的值
foreach (var item in ht)
{
MessageBox.Show(item.ToString());
}
//直接遍历键值对
foreach (DictionaryEntry item in ht)//这里的类型不能用var,一定要用DictionaryEntry
{
MessageBox.Show("键:" + item.Key + "值:" + item.Value);
}
集合练习
ArrayList arrayList = new ArrayList();
Random r = new Random();//Random对象一定要写在循环外面
while (arrayList.Count < 10)
{
int num = r.Next(1, 101);
if (num % 2 == 0 && !arrayList.Contains(num))
{
arrayList.Add(num);
}
}
foreach (var item in arrayList)
{
MessageBox.Show(item.ToString());
}
泛型集合List和Dictionary<key,value>
Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(0, "哈哈");
dic.Add(1, "嘿嘿");
//遍历键值对
foreach (KeyValuePair<int,string> item in dic)//使用KeyValuePair<>
{
MessageBox.Show("键:" + item.Key + " 值:" + item.Value);
}