码迷,mamicode.com
首页 > 编程语言 > 详细

数组和集合

时间:2017-06-12 22:36:37      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:threading   ash   task   hash   先进后出   ati   []   namespace   cti   

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[] arr1 = new int[2] {1,2};
int[,] arr2 = new int[2, 3] {
{0,1,2 },
{2,3,4 }
};

Console.WriteLine(arr2[1,1]);

ArrayList
ArrayList alt = new ArrayList();
alt.Add("123");
alt.Add(123);
alt.Add(true);
bool iscontain = alt.Contains("1");
alt.Clear();
/*alt.Insert(0, "abc")*/
;
Console.WriteLine(iscontain);

for(int i = 0; i < alt.Count; i++)
{
Console.WriteLine(alt[i].ToString() + " " + alt[i].GetType().ToString());
}

foreach (var x in alt)
{
Console.WriteLine(x.ToString() + " " + x.GetType().ToString());
}

泛型集合 List
List<string> str_list = new List<string>();
str_list.Add("a");
str_list.Add("b");
str_list.Add("c");
str_list.Add("d");

foreach(string x in str_list)
{
Console.WriteLine(x);
}


哈希表hashtable

Hashtable ht = new Hashtable();
ht.Add("1","a");
ht.Add(2, "b");
ht.Add(3, false);
ht.Add("x", 3.14);
Console.WriteLine(ht[2]);
foreach(var x in ht.Keys)
{
Console.WriteLine(ht[x]);
}

字典表 Dictionary

Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("a", 3);
dic.Add("b", 4);
dic.Add("c", 5);
dic.Add("d", 6);
dic.Add("e", 7);

foreach(var x in dic.Keys)
{
Console.WriteLine(x);
}

队列 queue

Queue que = new Queue();
que.Enqueue("张三");
que.Enqueue("李四");
que.Enqueue("王五");
que.Enqueue("赵六");
Console.WriteLine("现在的长度是" + que.Count);
Console.WriteLine(que.Dequeue());
Console.WriteLine("现在的长度是" + que.Count);

堆栈 stack

Stack st = new Stack();
st.Push("a");
st.Push("b");
st.Push("c");
st.Push("d");

Console.WriteLine(st.Count);
Console.WriteLine(st.Pop());
Console.WriteLine(st.Count);

Console.ReadLine();


int[] arr1 = new int[2] { 1, 2 };
int[,] arr2 = new int[2, 3]{
{0,1,2},{2,3,4}
};
Console.WriteLine(arr2[1, 1]);

ArrayList 特点不需要不需要限长度,不需要规定类型,缺点:键值只能是0,1,2,3往后排:

ArrayList alt = new ArrayList();
alt.Add("123");
alt.Add(123);
alt.Add(true);
alt.Remove(123); 从其中移除 clear 是全部清空的意思
bool iscontain=alt.Contains("123"); 看看里面是否包含"123",如果有就会显示"true",如果没有就会显示false;
alt.Clear(); 全部清除:
alt.Insert(0,"abc"); 意思是往 0 位置插入一个字符串"123";

Console.WriteLine(alt);
for (int i = 0; i < alt.Count;i++ ) {
Console.WriteLine(alt[i].ToString()+" "+alt[i].GetType().ToString());

}


var类型是 万能类型
foreach(var x in alt){

Console.WriteLine(x.ToString()+" "+alt[i].GetType().ToString)



}
泛型集合 list 需要规定类型,不需要规定长度 也不行规定键值 也是从0,1,2,3开始排

List<string> str_list = new List<string>();
str_list.Add("a");
str_list.Add("b");
str_list.Add("c");
str_list.Add("d");

foreach (string x in str_list) {

Console.WriteLine(x);


}


哈希表hashtable 不错在顺序的概念


Hashtable ht = new Hashtable();
ht.Add("1", "a");
ht.Add(2, "b");
ht.Add(3, false);
ht.Add("x", 3.14);
//Console.WriteLine(ht[2]);
foreach (var x in ht.Values) {

Console.WriteLine(x);


}
foreach (var x in ht.Keys)
{

Console.WriteLine(x); //取全部的值
Console.WriteLine(ht[x]);

}

 

字典表 Dictionary
Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("a", 3);
dic.Add("b", 4); 前面的是key键 后面的是value值
dic.Add("c", 5);
dic.Add("d", 6);
dic.Add("e", 7);
dic.Add("f", 8);


foreach (var x in dic.Keys) {

Console.WriteLine(x);
}


队列 queue 先进先出
Queue que = new Queue();
que.Enqueue("张三");
que.Enqueue("李四");
que.Enqueue("王五");
que.Enqueue("赵六");
Console.WriteLine("现在的长度是" + que.Count);
Console.WriteLine(que.Dequeue()); //dequeue()函数用于移除每个匹配元素的指定队列中的第一个函数,并执行被移除的函数。
Console.WriteLine("现在的长度" + que.Count);

堆栈 stack 先进后出


Stack st = new Stack();
st.Push("a");
st.Push("b");
st.Push("c");
st.Push("d");
st.Push("e");

Console.WriteLine(st.Count);
Console.WriteLine(st.Pop());
Console.WriteLine(st.Count);

 

Console.ReadLine();

}
}
}

数组和集合

标签:threading   ash   task   hash   先进后出   ati   []   namespace   cti   

原文地址:http://www.cnblogs.com/liuyubin0629/p/6993340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!