1、集合类(增、删、查、改、遍历)
a)集合常用操作 添加、遍历、移除 来自于 IList接口
b)使用时须先引用命名空间System.Collections
c)ArrayList 可变长度数组,使用类似于数组
属性 Capacity(容量) Count(实际元素个数)
方法
Add() AddRange() Remove() RemoveAt() Clear()
Contains() ToArray()
d)Hashtable 键值对的集合,类似于字典,Hashtable在查找元素的时候,速度很快。
Add(object key,object value);
hash[“key”]
hash[“key”]=“修改”;
ContainsKey(“key”);
Remove(“key”);
遍历:
hash.Keys
hash.Values/DictionaryEntry
2、ArrayList数组 动态数组
内部其实是通过一个 object[] 来储存数据
内部数组容量默认为4,每当储存超出容量的数据时,就会自动创建一个长度为2倍的新数组,并把老数组里的数据拷贝到新数组里
ArrayList arrlist = new ArrayList();
//1.1添加
string strArrayItem = "添加一件东西";
arrlist.Add(strArrayItem);//添加一个对象
int[] arrInt = new int[3] { 1, 2, 3 };
arrlist.AddRange(arrInt);//一次添加一堆对象集合数组
//1.2访问通过 下标索引来访问
arrlist[0] = "通过索引来访问集合里的元素";
Console.WriteLine(arrlist.Count);
//1.3删除
Program pObj2 = new Program();
arrlist.Remove(pObj2);//根据对象地址来删除
Console.WriteLine(arrlist.Count);
//arrlist.RemoveAt(0);//根据下标来删除
//arrlist.RemoveRange(0, 2);//根据范围来删除
//1.4遍历集合
for (int i = 0; i < arrlist.Count; i++)
{
Console.WriteLine(arrlist[i]);
}
//1.5枚举器遍历集合
Console.WriteLine("枚举器遍历集合Arraylist");
IEnumerator enutor = arrlist.GetEnumerator();
while (enutor.MoveNext())
{
object o = enutor.Current;
Console.WriteLine(o.ToString());
}
Console.WriteLine("枚举器遍历集合Arraylist");
3、Hashtable 哈希表
Hashtable ht = new Hashtable();
//2.1新增
ht.Add(1, "汪汪汪~~");
//ht.Add(1, "汪汪汪~~");//新增的键不能有重复值
ht.Add("花花", "汪汪汪2~~");
//2.2访问
ht["花花"] = "喵喵喵~~";
string valueStr = ht["花花"].ToString();//根据键来查找,注意,返回的是object类型对象
Console.WriteLine(valueStr);
//2.3删除
ht.Remove("花花");//根据键来删除
//2.4遍历²
ht.Keys
ht.Values
foreach (object key in ht.Keys)
{
Console.WriteLine(ht[key]);
}
//2.4.1使用枚举器(迭代器)来遍历HashTable
IDictionaryEnumerator enumrator = ht.GetEnumerator();
//调用枚举器的MoveNext()方法,从Hashtable中取出一个对象,存到Current 属性中
//同时,返回一个bool值,如果取到值,就返回true,如果没有取到值,就返回false
while (enumrator.MoveNext())
{
DictionaryEntry entry = (DictionaryEntry)enumrator.Current;//取出Current 属性中的值,并转成DictionaryEntry对象(键值对)
//取出当前循-环到的键和值
Console.WriteLine(entry.Key + ": " + entry.Value);
}
4、List<>泛型集合(ArrayList的泛型版本)
List<object> list = new List<object>();
5、泛型字典集合(Hashtable的泛型版本)
Dictionary<string, object> dict = new Dictionary<string, object>();
6、链表集合
LinkedList<string> li = new LinkedList<string>();
LinkedList<string> linkedList = new LinkedList<string>();
LinkedListNode<string> node = new LinkedListNode<string>("瑞奇");
linkedList.AddLast(node);
链表是通过节点来添加元素的
最大特点:集合本身只保存头节点,如果再添加第二个节点,其实是把第二个节点地址保存在头结点的next变量中,依此类推,要在LinkList中找到某个节点,都是从第一个节点开始遍历,直到找到要找到的节点为止
7、自动去除重复项的集合
HashSet<string> set = new HashSet<string>();
set.Add("瑞奇");
set.Add("瑞奇");
set.Add("瑞奇2");
Console.WriteLine(set.Count); 输出为瑞奇、瑞奇2
8、泛型集合性能比较
l 比较泛型集合List<T>与ArrayList的性能。循环增加整数。(装箱、拆箱 )见备注。
l //1.装箱、拆箱必须是: 值类型→引用类型 或 引用类型→值类型。
//object,接口。值类型是可以实现接口。
//Person p=new Student();//这个叫隐式类型转换,不叫装箱。
//Student stu=(Student)p;//这个叫显示类型转换,不叫拆箱。
//int类型为什么能装箱到object类型,但不能装箱到string类型或Person类型,
//因为object类型时int类型的父类。
//2.拆箱时,必须用装箱时的类型来拆箱
//装箱时如果是int,拆箱必须用int,不能用double
//3.方法重载时,如果具有该类型的重载,那么就不叫拆箱或装箱。
//4.字符串连接
// string.Concat(s1, s2, n3, d4);判断是否发生了装箱,及次数。
//总结:将值类型转换为引用类型(int→object)的时候,性能影响很大。一倍。
//装箱:将值类型转换为引用类型。
//拆箱:将引用类型转换为值类型。
//int n = 10;
//object o = n;//发生了一次装箱
//int m = (int)o;//发生了一次拆箱
//Console.WriteLine();
//1.装箱的时候是什么类型,拆箱的时候也用什么类型
//int n = 10;
//object obj = n;
//double d = (double)obj;//报错,只能用int来拆箱。
//Console.WriteLine(d);
//Console.ReadKey();
//int n = 10;
//object obj = n;
////double d = (double)((int)obj);√
////double d = Convert.ToDouble(obj);√
////double d = double.Parse(obj);×
////Console.WriteLine(d);
//Console.ReadKey();
//double d = 10.5;
//object obj = d;
//double d1 = (double)obj;
//Console.WriteLine(d1);
//Console.ReadKey();
//string s = "10";
//object obj = s;
//string x = (string)obj;//不是装箱或拆箱。(装箱和拆箱只限于值类型→引用类,引用类型→值类型。 )
//Console.WriteLine(x);
//int n = 10;
//string s = n;
//int n = 10;
//IComparable compar = n; //装箱。
//Console.WriteLine(compar.ToString());
//Console.ReadKey();
//总结:
//1.装箱、拆箱必须是: 值类型→引用类型 或 引用类型→值类型。
//object,接口。值类型是可以实现接口。
//Person p=new Student();//这个叫隐式类型转换,不叫装箱。
//Student stu=(Student)p;//这个叫显示类型转换,不叫拆箱。
//int类型为什么能装箱到object类型,但不能装箱到string类型或Person类型,
//因为object类型时int类型的父类。
//2.拆箱时,必须用装箱时的类型来拆箱
//装箱时如果是int,拆箱必须用int,不能用double
//3.方法重载时,如果具有该类型的重载,那么就不叫拆箱或装箱。
//4.字符串连接
// string.Concat(s1, s2, n3, d4);判断是否发生了装箱,及次数。
//应用:1.面试题2。系统中使用值类型的时候要注意装箱拆箱问题。(该考虑引用类型的时候就用引用类型。 )
//string s1 = "a";
//string s2 = "b";
//int n3 = 10;
//double d4 = 99.9;
//string result = string.Concat(s1, s2, n3, d4);
//Q1:有没有发生装箱? 有!
//Q2:如果有,发生了几次? 2
//Console.WriteLine(result);
int n = 10;
object o = n;
int m = (int)o;
Console.ReadKey();
//5.测试增加50000个student
//通过测试,发现,当存储引用类型数据的时候
//泛型集合(List<Person>)与非泛型集合ArrayList,性能差距缩小,因为
//没有发生装箱与拆箱。
//int x = 10;
//Test(x);//没有发生装箱
//double d = 9.9;
//Test(d);//发生了装箱,因为没有double的重载,所以调用了参数为object的方法。
原文:http://www.cnblogs.com/lcxBlog/p/4508864.html