集合
来源:互联网 发布:apache cgi 编辑:程序博客网 时间:2024/06/09 19:51
集合
一,集合
1, 命名空间:System.Collections
2, ArrayList:相当于动态数组
ArrayList list=new ArrayList();
ArrayList list=new ArrayList(10); //可以指定容量
常用方法:
方法
说明
Add
将对象添加到 ArrayList 的结尾处
Clear
从 ArrayList 中移除所有元素
Contains
确定某元素是否在 ArrayList 中
IndexOf
返回 ArrayList 或它的一部分中某个值的第一个匹配项的的索引
Insert
将元素插入 ArrayList 的指定索引处
LastIndexOf
返回 ArrayList 或它的一部分中某个值的最后一个匹配项的索引
Remove
从 ArrayList 中移除特定对象的第一个匹配项
RemoveAt
移除 ArrayList 的指定索引处的元素
Reverse
将 ArrayList 或它的一部分中元素的顺序反转
Sort
对 ArrayList 或它的一部分中的元素进行排序
A, 添加元素
Student s=new Student(“Jack”);
list.Add(s);
list.Isert(2,s); //在下标为2出插入s
注意:当存储的元素的个数超出ArrayList集合容量时,自动增长一倍
ArrayList集合允许Null值,并允许出现重复元素
删除元素
list.Clear(); //移除所有的元素
list.Remove(s); //从集合中删除特定的对象的第一个匹配项
list.RemoveAt(0); //移除指定下标处的元素
获取元素:和数组的操作一样,通过下标访问元素,下标从0开始
Object o=list[0];
注意:从集合中取出元素后要进行强制转换
遍历ArrayList集合:
for(int i=0;i<list.Count;i++)
{
Student s=list[0] as Student;
}
foreach(Object o in list)
{ }
3,ArrayList集合对应的泛型集合Lit< Type>
List<Student> list=new List<Student>();
Student s1=new Student(“Jack”);
Student s2=new Student(“Rose”);
list.Add(s1);
list.Add(s2);
Student s=list[0]; //不需要强制转换
4, ArrayList和List<T>泛型集合的对比:
ArrayList
List<T>
相同点
对集合数据的操作(增加、删除和获取)一致
不同点
存储任何类型数据
限定存储数据的类型
获取元素需要类型转换
获取元素时不需要类型转换
5,Hashtable:通过键/值对的方式存储数据,通过键获取值
Hashtable ht=new Hashtable ();
Hashtable常用属性和方法:
方法
说明
Add
将带有指定键和值的元素添加到 Hashtable 中
Clear
从 Hashtable 中移除所有元素
Contains
确定 Hashtable 是否包含特定键
ContainsKey
确定 Hashtable 是否包含特定键
ContainsValue
确定 Hashtable 是否包含特定值
Remove
从 Hashtable 中移除带有指定键的元素
属性
说明
Count
获取包含在 Hashtable 中的键/值对的数目
Keys
获取包含 Hashtable 中的键的 ICollection
Values
获取包含 Hashtable 中的值的 ICollection
添加元素:
ht.Add(“1”,”Jack”);
Student s=new Student(“Rose”);
ht.Add(s.Name,s);
注意:向Hashtable集合中添加元素时,键值不允许重复,否则会出现异常
Hashtable集合中的元素是无序的
获取元素
Student s=ht[“Rose”] As Student; //必须进行类型转换
删除元素:通过键值删除元素
list.Remove(“Rose”);
遍历Hashtable集合中的元素:
foreach(Object o in ht.Values)
{
Student s=o as student;
}
6,Hashtable对应的泛型集合Dictionary<K,V>
Dictionary<string,Student> dictionary=new Dictionary<string,Student>();
dictionary.Add(s.Name,s);
Hashtable和Dictionary的区别:
Hashtable
Dictionary<K,V >
相同点
对集合数据的操作(增加、删除和获取)一致
不同点
存储任何类型数据
限定存储数据的类型
获取元素需要类型转换
获取元素时不需要类型转换
7,SortedList:可以通过键值访问也可通过下标访问
SortedList常用方法;
方法
说明
Add
将带有指定键和值的元素添加到 SortedList 对象
Clear
从 SortedList 对象中移除所有元素
GetByIndex
获取 SortedList 对象的指定索引处的值
GetKey
获取 SortedList 对象的指定索引处的键
GetKeyList
获取 SortedList 对象中的键
GetValueList
获取 SortedList 对象中的值
IndexOfKey
返回 SortedList 对象中指定键的从零开始的索引
IndexOfValue
返回指定的值在 SortedList 对象中第一个匹配项的从零开始的索引
Remove
从 SortedList 对象中移除带有指定键的元素
RemoveAt
移除 SortedList 对象的指定索引处的元素
SetByIndex
替换 SortedList 对象中指定索引处的值
SortedList sl=new SortedList();
sl.Add(“1”,”Jack”);
sl.Add(“2”,s);
Student s=sl.GetByIndex(1) as Student; //需要类型转换
二, 集合排序
1, 实现IComparable<T>接口中的CompareTo方法
public class Student : IComparable<Student>
{
string name;
int age;
public void ShowInfo(){}
public int CompareTo(Student other)
{
if(this.age>other.age) return 1;
if(this.age==other.age) return 0;
if(this.age<other.age) return -1;
}
}
ArrayList list=new ArrayList();
list.Add(s1);
list.Add(s2);
list.Add(s3);
list.Sort(); //默认按年龄升序排列
IComparable<T>接口中的CompareTo()方法是集合在对元素排序时自动调用的,但存在的缺陷是只能按一种方式进行排序,若要按名称进行排序就无法进行了。要想实现按照多种方式排序可以使用IComparer<T>接口
2, 实现IComparer<T>接口中的Compare方法
Eg:
public class SortedByAge:IComparer<Student>
{
public int Compare(Student s1,Student s2)
{
if(s1.age>s2.age) return 1;
if(s1.age==s2.age) return 0;
if(s1.age<s2.age) return -1;
}
}
public class SortedByName:IComparer<Student>
{
public int Compare(Student s1,Student s2)
{
return s1.Name.CompareTo(s2.Name); //直接调用string的CompareTo方法
}
}
ArrayList list=new ArrayList();
list.Add(s1);
list.Add(s2);
list.Add(s3);
list.Sort(new SortedByAge()) //按年龄升序进行排序
list.Sort(new SortedByName()); //按姓名升序排列