C#学习笔记(十九):字典




自定义泛型


泛型类,第一替代符T,第二替代符U
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace m1w4d5_dictionary
{
class Program
{
static void Main(string[] args)
{
//把数字转换成中文字符
string inputStr = Console.ReadLine();
//123456789
//一二三四五
//字典<key,value>
//声明变量
Dictionary<char, string> dic = new Dictionary<char, string>();
dic.Add('', "一");
dic[''] = "二";
dic[''] = "三";
//修改一个元素
//dic.Add('1',"壹");位置已占,会报错
dic[''] = "贰";
//访问元素
//Console.WriteLine(dic['2']);
//遍历
foreach (var item in dic)
{
//Console.WriteLine(item);
if (item.Key == '')
{
Console.WriteLine(item.Value);
}
}
Console.WriteLine();
Dictionary<char, char> dic1 = new Dictionary<char, char>();
string num = "";
string str = "一二三四五六七八九零";
for (int i = ; i < num.Length; i++)
{
dic1[num[i]] = str[i];
}
for (int i = ; i < inputStr.Length; i++)
{
//如果用户输入的key不在我们的范围内,会报错
//通过ContainsKey判定字典中是否包含了对应的key
if (dic1.ContainsKey(inputStr[i]))
{
Console.Write(dic1[inputStr[i]]);
}
}
Console.WriteLine();
//计算字符串中每个字母出现的次数"Welcome to China! Welcome to HangKang!"
//我能通过字母(char)找到他出现的次数(int)
Dictionary<char, int> dic2 = new Dictionary<char, int>();
string str1 = "Welcome to China! Welcome to HangKang!";
for (int i = ; i < str1.Length; i++)
{
//如果你在字典有了,字典中有了这个字母
if (dic2.ContainsKey(str1[i]))
{
//你的值就自增
dic2[str1[i]]++;
}
else
{
//你的值就是
dic2[str1[i]] = ;
} }
foreach (var item in dic2)
{
Console.WriteLine(item.Key +":"+item.Value);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 泛型
{
//定义
//泛型是C#的一种特性,可以让我们将一种结构(类,接口)或者一种逻辑(函数)应用到所有类型
//如果我们要把一个类型或者方法写成泛型的,我们只需要在名称后面加上<>,<>中可以填入若干个类型替代符
class MyList<T>
{
T[] data;
public T this[int index]
{
get
{
if (index < || index >= count)
{
throw new OutOfMemoryException();
}
return data[index];
}
set
{
if (index < || index >= count)
{
throw new OutOfMemoryException();
}
else
{
data[index] = value;
}
}
}
public int Capacity
{
get { return data.Length; }
set
{
if (value == count) return;
if (value < count) value = count;
//建立新数组
T[] newData = new T[value];
//拷贝数据
for (int i = ; i < data.Length; i++) newData[i] = data[i];
//用新数组换老数组
data = newData;
}
}
int count = ;
public int Count => count;
//添加
public MyList(int capacity = )
{
data = new T[capacity];
}
//动态增长
public void Add(T item)
{
if (count >= Capacity) Capacity *= ;
data[count] = item;
count++;
}
//删除
public void Remove(T item)
{
int index = IndexOf(item);
if (index != -)
{
RemoveAt(index);
}
}
public void RemoveLast(T item)
{
int index = LastIndexOf(item);
if (index != -)
{
RemoveAt(index);
}
}
public void RemoveAt(int index)
{
if (index >= count)
{
throw new IndexOutOfRangeException();
}
for (int i = index + ; i < count; i++)
{
data[i - ] = data[i];
}
count--;
}
public void RemoveAll(T item)
{
while (true)
{
int index = IndexOf(item);
if (index != -)
{
RemoveAt(index);
}
else
{
break;
}
}
}
//查找
public int IndexOf(T item)
{
int index = -;
for (int i = ; i < count; i++)
{
if (item.Equals(data[i]))
{
return i;
}
}
return index;
}
public int LastIndexOf(T item)
{
int index = -;
for (int i = count - ; i >= ; i--)
{
if (item.Equals(data[i]))
{
return i;
}
}
return index;
}
public void Sort(Comparison<T> comparison)
{
for (int i = ; i < count - ; i++)
{
for (int j = ; j < count - - i; j++)
{
if (comparison(data[j], data[j + ]) > )
{
T temp = data[j];
data[j] = data[j + ];
data[j + ] = temp;
}
}
}
}
public void Sort(IComparer<T> iCompareer)
{
for (int i = ; i < count - ; i++)
{
for (int j = ; j < count - - i; j++)
{
if (iCompareer.Compare(data[j], data[j + ]) > )
{
T temp = data[j];
data[j] = data[j + ];
data[j + ] = temp;
}
}
}
}
public void Sort()
{
for (int i = ; i < count - ; i++)
{
for (int j = ; j < count - - i; j++)
{
IComparable<T> Icompar = data[j] as IComparable<T>;
if (Icompar.CompareTo(data[j + ]) > )
{
T temp = data[j];
data[j] = data[j + ];
data[j + ] = temp;
}
}
}
}
}
class Monster : IComparable<Monster>
{
public Monster(string name, int attack, int defend, int health)
{
this.name = name;
this.attack = attack;
this.defend = defend;
this.health = health;
}
public string name;
public int attack;
public int defend;
public int health;
public override string ToString()
{
return string.Format("{0}:[attack:{1},defend:{2},health{3}]", name, attack, defend, health);
}
public int CompareTo(Monster other)
{
//比较某一个参数,返回对应值
//如果大就返回大于0的数 自已排在对比参数的后面
//如果小就返回小于0的数 自已排在对比参数的前面
//如果相等就返回0 不换
//这样在外部调用Sort的时候会 形成一个以这个参数为标准的升序排序
return attack - other.attack;
}
public static int AttackSort(Monster a, Monster b)
{
return a.attack - b.attack;
}
public static int DefendSort(Monster a, Monster b)
{
return a.defend - b.defend;
}
}
class Program
{
static void Main(string[] args)
{
//List<int> list = new List<int>();
//list.Add(5);
//list.Add(6);
//list.Add(7);
//list.Capacity = 0;
//Console.WriteLine(list.Capacity);
MyList<Monster> myList = new MyList<Monster>();
Random roll = new Random();
//Console.WriteLine(myList.Capacity);
for (int i = ; i < ; i++)
{
myList.Add(new Monster(i.ToString(), roll.Next(, ), roll.Next(, ), roll.Next(, )));
}
//myList[5] = 1;
//myList[7] = 1;
myList.Sort();
for (int i = ; i < myList.Count; i++)
{
Console.WriteLine(myList[i]);
}
}
}
}
C#学习笔记(十九):字典的更多相关文章
- python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法
python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法 同一台机器同时安装 python2.7 和 python3.4不会冲突.安装在不同目录,然 ...
- (C/C++学习笔记) 十九. 模板
十九. 模板 ● 模板的基本概念 模板(template) 函数模板:可以用来创建一个通用功能的函数,以支持多种不同形参,进一步简化重载函数的函数体设计. 语法: template <<模 ...
- python 学习笔记十九 django深入学习四 cookie,session
缓存 一个动态网站的基本权衡点就是,它是动态的. 每次用户请求一个页面,Web服务器将进行所有涵盖数据库查询到模版渲染到业务逻辑的请求,用来创建浏览者需要的页面.当程序访问量大时,耗时必然会更加明显, ...
- Java基础学习笔记十九 IO
File IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再 ...
- Java基础学习笔记十九 File
IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再把这些数据 ...
- 【Python】学习笔记十:字典
字典是Python中唯一的映射类型(哈希表) 字典的对象时可变的,但字典的键值必须是用不可变对象,并且一个字典中可以使用不同类型的键值 1.定义字典 dict={key1:value1,key2:va ...
- JavaScript权威设计--跨域,XMLHttpRequest(简要学习笔记十九)
1.跨域指的是什么? URL 说明 是否允许通信 http://www.a.com/a.jshttp://www.a.com/b.js 同一域名下 允许 http://www.a.com/lab/a. ...
- SharpGL学习笔记(十九) 摄像机漫游
所谓的摄像机漫游,就是可以在场景中来回走动. 现实中,我们通过眼睛观察东西,身体移动带动眼睛移动观察身边的事物,这也是在漫游. 在OpenGL中我们使用函数LookAt()来操作摄像机在三维场景中进行 ...
- yii2源码学习笔记(十九)
view剩余代码 /** * @return string|boolean the view file currently being rendered. False if no view file ...
- PHP学习笔记十九【析构函数】
<?php class Person{ public $name; public $age; public function __construct($iname,$iage) { $this- ...
随机推荐
- 【其他】csv文件打开是乱码,怎么办?
csv文件打开是乱码,怎么办?管用的方法,一个就够 工作中,将python生成的中间结果文件写入CSV,经常这么干是不是?文件保存下来后用excel打开,出现了乱码情况,真心烦.为什么? CSV是用U ...
- Sql Server索引的原理与应用
SqlServer索引的原理与应用 转自:http://www.cnblogs.com/knowledgesea/p/3672099.html 索引的概念 索引的用途:我们对数据查询及处理速度已成 ...
- windows平台mysql密码设置
登录mysql默认没有指定账号 查看默认账号是谁 select user(); mysql> select user();+----------------+| user() |+------- ...
- 《深入理解Android内核设计思想》已陆续在全国各大书店及网上书店上市,感谢大家一直以来的支持~~
<深入理解Android内核设计思想>已陆续在全国各大书店上市,电子书店也在陆续上架中(不断添加): 1. China-Pub 2. 京东 3. s=books&ie=UTF8&a ...
- [django]celery_redis探索
celery+redis能做什么及简单原理 能干嘛: 看这里http://yshblog.com/blog/163 https://segmentfault.com/a/119000001565487 ...
- PAT 1034 Head of a Gang[难][dfs]
1034 Head of a Gang (30)(30 分) One way that the police finds the head of a gang is to check people's ...
- js实现轮播图2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- mysql日志详解
日志分类: 一.错误日志. 1.在配置文件中的配置是:log-error="DESKTOP-igoodful.err",查看参数的键值对:show variables like ' ...
- MySQL从删库到跑路(六)——SQL插入、更新、删除操作
作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.插入数据 1.为表的所有字段插入数据 使用基本的INSERT语句插入数据要求指定表名称和插入到新记录的值. IN ...
- 错误处理:WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping
今天在配置用户权限管理的时候,遇到了这么个错误: WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping.请添加一个名为 ...