//非泛型数列取出来都为 objec
//栈 对列 数组 都可以纯空数据
//Stack 栈 数据结构n 可被继承
int[ ] numbers = {1,2,3,5,6,7};
string[ ] str = {"li","ik","op"};
Stack stack = new Stack(str); //动态分配 初始为0 可以继续加入 自定义容量大小
//入栈 push
stack.Push(1);
//出栈 pop (移除并返回该值)
int number = (int)stack.Pop(); //强转类型
object obj = stack.Pop();
Console.WriteLine (obj.ToString()); //op
//获取栈顶元素 peek(不会移除)
object obj_1 = stack.Peek();
Console.WriteLine (obj_1); //ik
object obj_2 = stack.Peek(); //ik
Console.WriteLine (obj_2);
//是否包含某个元素
stack.Contains("2");
//转为数组
object[ ] objs = stack.ToArray();
//获取栈的长度
int count = stack.Count;
//输出栈里的所有元素
for (int i = 0; i < count; i++) {
stack.Pop ();
}
//Queue 对列
Queue queue = new Queue(stack); //默认或初始容量
Queue<int>queue_1 = new Queue(); //泛型定义为int 只能传入int类
//入对列
queue.Enqueue("wang");
queue_1.Enqueue (5);
//出对列
object ob = queue.Dequeue();
int num = queue_1.Dequeue ();
//获取对列头部元素
object ob_1 = queue.Peek();
int num_1 = queue_1.Peek();
Console.WriteLine (ob_1);
//对列元素个数
queue_1.Count;
queue.Count;
//移除非泛型多余空间
queue.TrimToSize();
//Dictionary 字典 值可以重复 键不行
Dictionary<string,int>dic = new Dictionary<string,int>();
dic.Add ("One", 1); //给dic添加对应键值
dic.Add("two",2);
//获取对应数据
int number_1 = dic["One"]; //通过one 得到对应值 1
Console.WriteLine(number_1); // 1
//修改对应值
dic["One"] = 3;
int number_2 = dic ["One"]; // 3
Dictionary<string,string>dic_1 = new Dictionary<string,string>();
dic_1 ["li"] = "an"; //自动添加一个新的
dic_1 ["li"] = null;
Console.WriteLine (dic_1.Count);
dic_1.Remove ("li");
Console.WriteLine (dic_1.Count);
//是否包含指定的数
if (dic.ContainsKey("Three")) {
Console.WriteLine ("sXXXXX");
}
//是否包含指定的数
if (dic.ContainsValue(2)) {
Console.WriteLine ("value is 2");
}
foreach (string key in dic.Keys) {
Console.WriteLine (key + ":" + dic[key]);
}
foreach (int value in dic.Values) {
Console.WriteLine (value);
}
//用户输入10个姓名,可以从父输入同一个姓名,输出多少个姓名并输出多少次
Dictionary<string,int>dic = new Dictionary<string,int>();
Console.WriteLine ("请输入十次姓名");
for (int i = 0; i < 10; i++) {
string str = Console.ReadLine ();
if (dic.ContainsKey (str)) {
dic [str] += 1;
} else {
dic [str] = 1;
}
}
foreach (string name in dic.Keys) {
Console.WriteLine (name + "次数为" + dic[name]);
}
//ArrayList (非泛型) 数组列表 引用类型
int[ ] numbers = {1,2,3,4,5,6};
// ArrayList arrayList = new ArrayList(); //默认构造 长度为0
ArrayList arrayList = new ArrayList(numbers);
//添加
arrayList.Add("Tom");
arrayList.AddRange (new int[]{2,4,5}); //添加一个数组
//移除
arrayList.Remove(3); //移除指定字符
foreach (object obj in arrayList) {
Console.WriteLine (obj); //1 2 4 5 6 Tom
}
arrayList.RemoveAt (2); //移除指定下标
arrayList.RemoveRange(1,2); //移除下标为1 及其后面两位
//得到逆序数组
arrayList.Reverse(); //对整个数组进行逆序
//从下标2开始截取两位
ArrayList arr = arrayList.GetRange (2,2);
//获取字符的下标
int index = arrayList.IndexOf ("Tom");
//获取最后一个Tom 的下标
arrayList.LastIndexOf ("Tom");
//从下标2开始改变 (范围内改变)
arrayList.SetRange (2,new string[]{"li","zhang"});
//List(泛型) 与arrayList类似
List<int>numbers = new List<int>(new int[ ]{11,31,5,7,89});
//排序 从小到大
numbers.Sort ();
foreach (var n in numbers) {
Console.WriteLine (n);
}
索引器
private Dictionary<string,int>info = new Dictionary<string,int>();
//索引器(类中)
public class StudentInfo
{
public int this [string key]{
get{
if (info.ContainsKey (key)) {
return info [key];
} else {
return -1;
}
}
set{
info [key] = value;
}
}
}
外部main调用索引器
StudentInfo stuInfo = new StudentInfo();
stuInfo ["limi"] = 89;
stuInfo ["xiaoming"] = 60;
Console.WriteLine (stuInfo["xiaohong"]); //找不到 返回值-1
Console.WriteLine (stuInfo["limi"]); //找到 返回89
9_7 ---9_8委托