System.Array 定长数组,索引查找速度快,固定长度的一段内存.存储类型一致 参考文档: http://msdn.microsoft.com/en-us/library/system.array(v=vs.110).aspx
using System;
namespace TestCSharp
{
#region MainCalss
class MainClass
{
public static void Main(string[] args)
{
//初始
int[] charList = new int[100];
//charList[100] = 200;Error
string[] nameList = { "Bob", "Conie", "Jennifer" };
int[,] mapList = new int[3, 4];//mapList.Length = 12;
int[,] maskList = { { 1, 0, 1 }, { 4, 0, 5 }, { 6, 0, 7 }, { 16, 0, 17 } };
string[][] itemList = new string[5][];
itemList[4] = new string[3] { "a", "b", "c" };
itemList[2] = new string[4] { "a", "b", "c", "d" };
//遍历
foreach (string t in nameList)
{
Console.WriteLine(t);
}
for (int i = 0; i < nameList.Length; i++)
{
Console.WriteLine(nameList[i]);
}
foreach (var s in maskList)
{
Console.WriteLine(s);
}
for (int i = 0; i < itemList.Length; i++)
{
if (itemList[i] != null)
{
Console.WriteLine("i:{0},length:{1}",i,itemList[i].Length);
}
}
}
}
#endregion
}
在for遍历时,不推荐上面的写法,可修改为如下
int total = nameList.Length;
for (int i = 0; i < total; i++)
{
Console.WriteLine(nameList[i]);
}
System.Collections.ArrayList,可以理解为可以动态增加内存的数组,是一种链表 。参考文档: http://msdn.microsoft.com/zh-cn/library/system.collections.arraylist_methods(v=vs.80).aspx
1. 支持自动改变大小的功能 2. 可以灵活的插入元素 3. 可以灵活的删除元素 4. 可以灵活访问元素 5.存储类型可以不一致,因为把不同的类型都当做Object来做处理,有拆箱和封箱的效率损耗。
using System;
using System.Collections;
namespace TestCSharp
{
#region MainCalss
class MainClass
{
public static void Main(string[] args)
{
ArrayList nameList = new ArrayList();
nameList.Add("Bob");
nameList.Remove("Bob");
Console.WriteLine(nameList.Count);
Console.WriteLine(nameList.Contains("Bob"));
for (int i = 0; i < 10; i++)
{
nameList.Add("Unkowned");
}
nameList.Insert(1,"Conie");
nameList.Capacity = 50;
ArrayList otherList = new ArrayList(new string[]{"Jennifer","Ben"});
nameList.InsertRange(3,otherList);
Console.WriteLine(nameList.IndexOf("Ben"));//4
nameList.RemoveAt(1);
nameList.Reverse();
foreach (var name in nameList)
{
Console.WriteLine(name);
}
Console.WriteLine("");
nameList.Sort();
for (int i = 0; i < nameList.Count; i++)
{
Console.WriteLine(nameList[i]);
}
nameList.Add(123);
nameList.Clear();
}
}
#endregion
}
System.Collections.Generic.Dictionary 参考文档: http://msdn.microsoft.com/en-US/library/xfhwa508(v=vs.80).aspx
using System;
using System.Collections;
using System.Collections.Generic;
namespace TestCSharp
{
#region MainCalss
class MainClass
{
public static void Main(string[] args)
{
Dictionary<int, string> cachDic = new Dictionary<int, string>();
cachDic.Add(1000, "Jennifer");
Console.WriteLine(cachDic.Count);
Console.WriteLine(cachDic.ContainsKey(1000));
Console.WriteLine(cachDic.ContainsValue("Bob"));
foreach (KeyValuePair<int, string> keyValuePair in cachDic)
{
Console.WriteLine("key:{0},value:{1}",keyValuePair.Key,keyValuePair.Value);
}
foreach (var name in cachDic)
{
Console.WriteLine(name);//[1000,Jennifer]
Console.WriteLine(name.Key);//1000
Console.WriteLine(name.Value);//Jennifer
}
foreach (var key in cachDic.Keys)
{
Console.WriteLine(key);//1000
}
foreach (var value in cachDic.Values)
{
Console.WriteLine(value);//Jennifer
}
cachDic.Clear();
}
}
#endregion
}
Stack 表示对象的简单的后进先出非泛型集合 参考文档: http://msdn.microsoft.com/en-US/library/system.collections.stack(v=vs.80).aspx
using System;
using System.Collections;
namespace TestCSharp
{
#region MainCalss
class MainClass
{
public static void Main(string[] args)
{
Stack s = new Stack();
s.Push("Bob");
s.Push("Conie");
s.Push("Jennifer");
Console.WriteLine(s.Pop());//Jennifer
}
}
#endregion
}
Queue 表示对象的先进先出集合 参考文档: http://msdn.microsoft.com/en-us/library/system.collections.queue_members(v=vs.80).aspx
using System;
using System.Collections;
namespace TestCSharp
{
#region MainCalss
class MainClass
{
public static void Main(string[] args)
{
Queue q = new Queue();
q.Enqueue("Bob");
q.Enqueue("Conie");
q.Enqueue("Jennifer");
Console.WriteLine(q.Dequeue());//Bob
}
}
#endregion
}
System.Collections.Generic.List ,是一种范型链表,解决了ArrayList对于值类型需要装箱拆箱的缺点 。需要处理的元素数量不确定时 通常建议使用
参考文档: http://msdn.microsoft.com/en-us/library/d9hw1as6(v=vs.80).aspx
System.Collections.Generic.LinkedList
链表适合元素数量不固定,需要经常增减节点的情况,2端都可以增减
参考文档: http://msdn.microsoft.com/en-us/library/he2s3bh7(v=vs.80).aspx