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

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

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

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

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

自定义泛型

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

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]);
}
}
}
}
上一篇:20155237 2016-2017-2 《Java程序设计》第8周学习总结


下一篇:[外挂6]在指定位置下棋 SendMessage函数