集合
ArrayList
1、 要引用system.Collections
2、 ArrayList内部其实就是用1个object数组来存储数据
3、 在这个类的静态构造函数中实例化了这个object’数组,长度为0
4、 如果第一次往这个动态数组增加元素,那么就把数组的程度为4
5、 如果是第二次以后扩容 那么数组的长度就是原来的2倍
6、 ArrList是用object数组来存储来存数组,所以我们取值的时候 取出来的变量类型是一个object类型 需要类型转换
7、 增加数据 Add方法
8、 Count属性表示ArrList集合中有效元素的个数
9、 Capacity属性代表items数组的长度
10、 实现ICollection接口的类可以使用AddRange()方法
11、 取值 通过索引器下标来取值 list[1]
12、 修改值 通过索引器下标来修改值
13、 删除 根据下标来删除 list.RemoveAt(2) 删除的时候后面的元素会往前占据 并且元素的Count会发生变化,只删除第一个,如果有多个引用类型的变量在集合众,并且这几个应用类型变量指向同一个对象的时候,删除其中一个,只会删除变量,其对象还在 Remove()则是删除对象 RemoveRange(int index ,int count)是从某个下表开始删除一直删除count个元素
14、 清空 list.Clear(); 元素的值设置为null,集合的容量不变
15、 取值 修改值 删除 传索引的时候不能越界(0~conunt—1)
16、 Contains() 用来判断是否包含某个值
17、 ToArray()把集合里面的数组拷贝一份到新的数组
18、 ArrayList list1 = new ArrayList(){"a","b","c","d","e"};可以这样初始化
19、 在某个位置插入list1.Insert(int index,value)
哈希
Hashtable ht = new Hashtable();
ht.Add("name", "小明");//增
ht["name"].ToString();//取
ht["name"]=”小花”
1、 存到一个叫bucket类型的结构体(结构体里面有value 、key、 哈希码)数组里面
2、 添加键值对的时候 会根据键的值和bucket来算出这个键值对应该存储在数组中的位置
3、 取值的时候(会根据哈希算法(key、bucket的长度)来算出这个键所在的下标,通过key去取值,如果找的key不存在则会返回null;
4、 Key的值不能相同于不能为null’
5、 Hashtable是引用类型
6、 Count属性用来保存元素的个数ht.Count
7、 判断有木有指定的键 ht.Contains(“name”)
8、 判断是否包含某个值ht.ContainsValue(p)
Person p = new Person();
Person p1 = p;
Console.WriteLine(ht.ContainsValue(p1));
注意这里输出是真
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add("name", "小明");//增
ht.Add("name1", "小明1");//增
ht.Add("name2", "小明2");//增
ht.Add("name3", "小明3");//增
ht.Add("name4", "小明4");//增
ht["name"].ToString();//取
Person p = new Person();
Person p1 = p;
ht.Add("name5", p);
Console.WriteLine(ht.ContainsValue(p1));//输出为真,哈希表里面存的是地址(p和批
//p1指向同一个对象,所以地址相同),所以输出为真
//遍历出来的东西可能与存的顺序不一样
//遍历哈希表的一种方法
foreach (var item in ht.Values)
{
Console.WriteLine(item);
}
Console.WriteLine("************");
//遍历哈希表的第二种方法
foreach (var item in ht.Keys)
{
Console.WriteLine(ht[item]);
}
Console.ReadKey();
}
class Person
{
}
}
泛型
List<T>
//尖括弧中写指定的数据类型 代表list对象中 items数组的数据类型
List<int> iss = new List<int>();
List<Person> ps = new List<Person>();
iss.Add(123);
//其他方法用于ArrayList一样 可以说list是ArrayList的泛型版
Dictionary<TKey, TValue>
Person p = new Person();
Person p1 = new Person();
Person p2 = new Person();
Dictionary<string, Person> dic = new Dictionary<string, Person>();
dic.Add("sas", p);
dic.Add("ee", p1);
//取出来的值就是Person类型
//其他方法用于Hashtable一样,但其还有其独有的遍历方法,把键值对都取出来
foreach (KeyValuePair<string ,Person> kv in dic)
{
Console.WriteLine(kv.Key);
kv.Value.SayHi();
}
LinkedList 链表集合
//泛型链表集合
LinkedList<Person> li = new LinkedList<Person>();
li.AddFirst(p);//把这个节点作为这个链表的开始
//每一个节点都是LinkedListNode类型
//li[0]会报错 是根据节点来取值的而不是索引
LinkedListNode<Person> node1 = li.AddFirst(p);
LinkedListNode<Person> node2 = li.AddAfter(node1, new Person());//在某个节点之后创建新的节点
node1.Value.SayHi();
node1.Next.Value.SayHi();//获取node1下一个节点
li.AddAfter(node1, new Person());//在node1后加一个节点,这个时候node2排第三了
li.AddFirst(new Person());//原来的依次往后退
LinkedListNode<Person> nodefirst = li.First;//获取第一个节点
LinkedListNode<Person> nodeend = li.Last;//获取最后一个节点
//遍历
while (nodefirst != null)
{
nodefirst.Value.SayHi();
nodefirst = nodefirst.Next;
}