1、哈希表:
//哈希表
Hashtable student = new Hashtable();
for (int i = 0; i < 10; i++)
{
string key = "sk" + i.ToString();
string value = "sv" + i.ToString();
if (!student.ContainsKey(key))
{
student.Add(key, value);
}
}
//根据键移除元素
student.Remove("sk3");
foreach (DictionaryEntry element in student)
{
string id = element.Key.ToString();
string name = element.Value.ToString();
Debug.WriteLine("学生的ID:" + id + ",学生的姓名:" + name);
}
//学生的ID:sk9,学生的姓名:sv9
//学生的ID:sk8,学生的姓名:sv8
//学生的ID:sk5,学生的姓名:sv5
//学生的ID:sk6,学生的姓名:sv6
//学生的ID:sk7,学生的姓名:sv7
//学生的ID:sk4,学生的姓名:sv4
//学生的ID:sk1,学生的姓名:sv1
//学生的ID:sk0,学生的姓名:sv0
//学生的ID:sk2,学生的姓名:sv2
2、ArrayList:
ArrayList arrlist = new ArrayList();
arrlist.Add("item0");
arrlist.Add("item1");
arrlist.Add("item2");
arrlist.Add("item3");
foreach (int n in new int[3] { 0, 1, 2 })
{
arrlist.Add(n);
}
//arrlist.Remove(0);//移除值为0的第一个元素
//arrlist.RemoveAt(3);//移除index为3的元素
//arrlist.Insert(1, "apple");//在index为1的位置插入元素
for (int i = 0; i < arrlist.Count; i++)
{
Debug.WriteLine(arrlist[i].ToString());
}
3、HashSet:
HashSet<string> hSet = new HashSet<string>();
hSet.Add("item0");
hSet.Add("item1");
hSet.Add("item0");
hSet.Add("item0");
foreach (string item in hSet)
{
Debug.WriteLine("foreach=" + item);
}
//foreach=item0
//foreach=item1
4、List:
//列表
//list.
List<string> list = new List<string>();
list.Add("item0");
list.Add("item1");
list.Add("item2");
for (int i = 0; i < list.Count; i++)
{
Debug.Write("list[" + i + "]=" + list[i] + "\n");
}
list.RemoveAt(0);
Debug.Write("删除掉一个元素\n");
for (int j = 0; j < list.Count; j++)
{
Debug.Write("list[" + j + "]=" + list[j] + "\n");
}
Debug.Write("或者用foreach来遍历\n");
foreach (string item in list)
{
Debug.Write("foreach元素=" + item + "\n");
}
Debug.Write("添加数组\n");
list.AddRange(new string[] { "添加元素1", "添加元素2", "添加元素3" });
list.ForEach(ShowItem);
private static void ShowItem(string item)
{
Debug.Write("添加后遍历的item=" + item + "\n");
}
5、Queue:
//队列
//先进先出
Queue<string> queue = new Queue<string>();
queue.Enqueue("item0");
queue.Enqueue("item1");
queue.Enqueue("item2");
queue.Enqueue("item3");
foreach (string item in queue)
{
Debug.Write("foreach queue=" + item + "\n");
}
Debug.Write("__________________用while方式_________________\n");
while (queue.Count > 0)
{
Debug.Write("while=" + queue.Dequeue() + "\n");
}
6、stack:
//栈
//先进后出
Stack<string> stack = new Stack<string>();
stack.Push("item0");
stack.Push("item1");
stack.Push("item2");
stack.Push("item3");
foreach (string item in stack)
{
Debug.Write("stack-foreach:" + item + "\n");
}
Debug.Write("__________________用while方式_________________\n");
while (stack.Count > 0)
{
Debug.Write("stack-while:" + stack.Pop() + "\n");
}
7、LinkedList:
//链表
LinkedList<string> lList = new LinkedList<string>();
LinkedListNode<string> node = new LinkedListNode<string>("root");
lList.AddFirst(node);
node = lList.AddAfter(node, "item0");
node = lList.AddAfter(node, "item1");
node = lList.AddAfter(node, "item2");
node = lList.AddAfter(node, "item3");
foreach (string item in lList)
{
Debug.WriteLine("foreach-lList:" + item);
}
node = lList.First;
Debug.WriteLine("第一个元素:" + node.Value);
node = lList.Last;
Debug.WriteLine("最后一个元素:" + node.Value);
MyList<string> list = new MyList<string>();
for (int i = 0; i < 10; i++)
{
list.AddHead("item" + i);
}
foreach (string item in list)
{
Debug.WriteLine(item);
}
//item9
//item8
//item7
//item6
//item5
//item4
//item3
//item2
//item1
//item0
MyList<int> list = new MyList<int>();
for (int i = 0; i < 10; i++)
{
list.AddHead(i);
}
foreach (int item in list)
{
Debug.WriteLine(item);
}
//9
//8
//7
//6
//5
//4
//3
//2
//1
//0
8、SortedList:
SortedList<int, string> sList = new SortedList<int, string>();
sList.Add(5, "item0");
sList.Add(25, "item1");
sList.Add(24, "item2");
sList.Add(475, "item3");
foreach (KeyValuePair<int, string> item in sList)
{
//根据key的顺序来
Debug.WriteLine("key=" + item.Key.ToString() + ",value=" + item.Value);
}
9、Dictionary:
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(141, "item0");
dict.Add(46, "item1");
dict.Add(541, "item2");
dict.Add(21, "item3");
foreach (KeyValuePair<int, string> item in dict)
{
//按添加的顺序来
Debug.WriteLine("key=" + item.Key.ToString() + ",value=" + item.Value);
}
dict.containsKey("xxx");//判断有没有包含这个key
dict.containsValue("xxx");
排序,从小到大:
Dictionary<int, string> dictSorted = dict.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
foreach (KeyValuePair<int, string> kv in dictSorted)
{
Debug.WriteLine("keyNew=" + kv.Key + " valNew=" + kv.Value);
}
从大到小:
Dictionary<int, string> dic1desc = test.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
用for循环遍历:
List<int> indexList = new List<int>(dictSorted.Keys);
// 按照新的dict来获取值
for (int i = 0; i < dictSorted.Count; i++)
{
int index = indexList[i];
string name = dictSorted[i];
}
10、SortedDictionary:
SortedDictionary<int, string> dict = new SortedDictionary<int, string>();
dict.Add(23, "item0");
dict.Add(54, "item1");
dict.Add(132, "item2");
dict.Add(12, "item3");
foreach (KeyValuePair<int, string> item in dict)
{
//按key的顺序来
Debug.WriteLine("key=" + item.Key.ToString() + ",value=" + item.Value);
}
//key=12,value=item3
//key=23,value=item0
//key=54,value=item1
//key=132,value=item2
11、DataTable:
DataTable dataTable = MySQLiteHelper.ExecuteDataTable(sqlText);
for (int i = 0; i < dataTable.Rows.Count; i++)
{
for (int j = 0; j < dataTable.Columns.Count; j++)
{
Debug.WriteLine(dataTable.Rows[i][dataTable.Columns[j].ColumnName]);
}
}