自定义泛型

泛型类,第一替代符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#学习笔记(十九):字典的更多相关文章

  1. python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法

    python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法 同一台机器同时安装 python2.7 和 python3.4不会冲突.安装在不同目录,然 ...

  2. (C/C++学习笔记) 十九. 模板

    十九. 模板 ● 模板的基本概念 模板(template) 函数模板:可以用来创建一个通用功能的函数,以支持多种不同形参,进一步简化重载函数的函数体设计. 语法: template <<模 ...

  3. python 学习笔记十九 django深入学习四 cookie,session

    缓存 一个动态网站的基本权衡点就是,它是动态的. 每次用户请求一个页面,Web服务器将进行所有涵盖数据库查询到模版渲染到业务逻辑的请求,用来创建浏览者需要的页面.当程序访问量大时,耗时必然会更加明显, ...

  4. Java基础学习笔记十九 IO

    File IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再 ...

  5. Java基础学习笔记十九 File

    IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再把这些数据 ...

  6. 【Python】学习笔记十:字典

    字典是Python中唯一的映射类型(哈希表) 字典的对象时可变的,但字典的键值必须是用不可变对象,并且一个字典中可以使用不同类型的键值 1.定义字典 dict={key1:value1,key2:va ...

  7. JavaScript权威设计--跨域,XMLHttpRequest(简要学习笔记十九)

    1.跨域指的是什么? URL 说明 是否允许通信 http://www.a.com/a.jshttp://www.a.com/b.js 同一域名下 允许 http://www.a.com/lab/a. ...

  8. SharpGL学习笔记(十九) 摄像机漫游

    所谓的摄像机漫游,就是可以在场景中来回走动. 现实中,我们通过眼睛观察东西,身体移动带动眼睛移动观察身边的事物,这也是在漫游. 在OpenGL中我们使用函数LookAt()来操作摄像机在三维场景中进行 ...

  9. yii2源码学习笔记(十九)

    view剩余代码 /** * @return string|boolean the view file currently being rendered. False if no view file ...

  10. PHP学习笔记十九【析构函数】

    <?php class Person{ public $name; public $age; public function __construct($iname,$iage) { $this- ...

随机推荐

  1. CentOS 6.5通过yum的方式安装MySql

    一.mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库,咱 ...

  2. Java编写验证码

    Java后台代码(CheckCodeServlet.java) package web; import java.awt.Color; import java.awt.Font; import jav ...

  3. Java: Best Way to read a file

    经常在各种平台的online test里面不熟悉STDIN, STOUT,下面举个例子: Input Format There are three lines of input: The first ...

  4. css样式属性-字体和隐藏

    1.字体 font-family:字体: <body> <div style="font-family:宋体">宋体</div> </bo ...

  5. stdcall cdecl

    一.stdcall windows API采用的都是这种方式 1.参数入栈由右向左 2.栈平衡由被调用者处理 二.cdcel C语言库采用的都是这种方式 1.参数入栈由右向左 2.栈平衡由调用者处理 ...

  6. JSON自动生成相关类

    开源项目地址:https://jsonclassgenerator.codeplex.com/SourceControl/latest 太好用了,这个

  7. python 内置函数bytearray

    1.参考文档 class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cl ...

  8. vue 拨打电话

    <a v-bind:href="'tel:'+(order.orderer.phone)">{{order.orderer.phone}}</a> v-bi ...

  9. 网络营销相关缩写名称CPM CPT CPC CPA CPS SEM SEO解析

    网络营销相关缩写名称CPM CPT CPC CPA CPS SEM SEO解析 CPM CPT CPC CPA CPS SEM SEO在网络营销中是什么意思?SEO和SEM的区别是? CPM(Cost ...

  10. mongodb 最佳可视化工具mongobooster

    最好用的mongodb GUI工具 mongobooster,没有之一,可从https://mongobooster.com/下载 常见管理命令可参考,http://www.cnblogs.com/l ...