class Program
{
int a = 0;
int b = 10;
static void Main(string[] args)
{
//------------------哈希表--------------------
Hashtable hash = new Hashtable();//哈希表只能传两个参数 键 值
hash.Add(1, "苹果");
hash.Add(2, "华为");//键不能重复,值可以重复;
hash.Add(5, "三星");
hash.Add(3, 1);
hash.Add(4, "1");
hash.Add(6, "锤子");
foreach (int n in hash.Keys)//遍历键
{
Console.WriteLine(n);
}
Console.WriteLine(hash.Count);//键值对 个数;
hash[3] = "小米";
Console.WriteLine(hash[3]);//通过键获得对象得值
foreach (string n in hash.Values)//遍历值
{
Console.WriteLine(n);
}
hash.ContainsValue(1);//查找指定值是否存在;有ture 无 flase
hash.ContainsKey("华为");//查找指定键是否存在;有ture 无 flase
//键值对的字典 成对出现 这个出现就是为了集合之用的;
foreach (DictionaryEntry dic in hash)
{
Console.WriteLine("键:{0} 值:{1}", dic.Key, dic.Value);
}
hash.Remove(3);//移除指定键对应的元素
Hashtable hs = new Hashtable();
hs.Add(11, "苹果");
hs.Add(88, "华为");
hs.Add(77, "appo");
hs.Add(99, "三星");
hs.Add(22, "鸿基");
hs.Add(44, "神舟");
hs.Add(55, "联想");
hs.Add(98, "红米");
foreach (DictionaryEntry n in hs)
{
Console.WriteLine("键:{0}---值:{1} ", n.Key, n.Value);
}
Console.WriteLine();
ArrayList list = new ArrayList(hs.Keys);
list.Sort();//排序后注意类型
list.Reverse();
list.Reverse();
foreach (int n in list) //注意int 的类型
{
Console.WriteLine("键:{0}---值:{1} ", n, hs[n]);//注意输出的值
}
//------------------排序列表---------------------------
SortedList sort = new SortedList();
sort.Add(11, "苹果");
sort.Add(88, "华为");
sort.Add(77, "appo");
sort.Add(99, "三星");
sort.Add(22, "鸿基");
sort.Add(44, "神舟");
sort.Add(55, "联想");
sort.Add(98, "红米");
for (int i = 0; i <= 18; i++)
{
sort.Add(i, "appo" + i);
}
Console.WriteLine(sort.Capacity);//起始容量是16 不写容量为零 之后 16的翻倍
foreach (int n in sort.Keys)
{
Console.WriteLine("键:{0} 值:{1}", n, sort[n]);
}
sort[0] = "888";//改变值;
Console.WriteLine(sort.GetByIndex(2));//返回下标的值
Console.WriteLine(sort.GetKey(2));//返回下标的键
foreach (int n in sort.GetKeyList())//返回下标键值 注意对象类型
{
Console.WriteLine("键{0}", n);
}
Console.WriteLine(sort.IndexOfKey(3));//返回的是键
SortedList sort = new SortedList();
sort.Add(1,"苹果");
sort.Add(2,"华为");
sort.Add(6,"appo");
sort.Add(4,"三星");
sort.Add(5,"小米");
Console.WriteLine(sort.ContainsKey(8));
Console.WriteLine(sort.ContainsValue("苹果"));
Console.WriteLine(sort.GetByIndex(0));//获取相应下标的值;苹果
Console.WriteLine(sort.GetKey(0));//获取相应下标的键: 1
for (int i = 0; i < sort.Count; i++)
{
Console.Write("键:{0}---值:{1} ", sort.GetKey(i), sort.GetByIndex(i));
}
foreach (int i in sort.GetKeyList())//获取键列表
{
Console.Write(i + " ");
}
Console.WriteLine();
foreach (string i in sort.GetValueList())//获取值列表
{
Console.Write(i + " ");
}
Console.WriteLine();
Console.WriteLine(sort.IndexOfKey(1));//指定键寻找返回下标 0
Console.WriteLine(sort.IndexOfValue("苹果"));//指定值寻找返回下标 0
sort.Remove(1);//移除指定下标的键值
foreach (DictionaryEntry dic in sort)//字典
{
Console.WriteLine("{0}----{1}", dic.Key, dic.Value);//键值对 注意输出的类型调用
}
//---------------监视窗口 局部变量 即时窗口 调用堆栈----------------
//设置断点 或者点f9 可以设置
//f11调试
Program pro = new Program();//实例化类 静态的类 抽象的类 接口的类不能被实例化
for (int i = 0; i < 2; i++)
{
pro.Jia();
Console.WriteLine(i);
}
Console.ReadKey();
}
public void Jia() {
a++;
b--;
}
}
unity游戏开发-C#语言基础篇(哈希表排序列表及断点调试)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。