集合类List与Dictonary实例练习
程序员文章站
2024-03-31 18:23:52
a、list<>泛型集合 复制代码 代码如下: view code using system; using system.collections.generic...
a、list<>泛型集合
view code
using system;
using system.collections.generic;
namespace _02_泛型集合 {
class person {
public person(string name, int age) {
this .name = name;
this .age = age;
}
private string name;
public string name {
get {
return name;
}
set {
name = value ;
}
}
private int age;
public int age {
get {
return age;
}
set {
age = value ;
}
}
}
class student : person {
public student(string name, int age)
: base (name, age) {
}
}
class teacher : person {
public teacher(string name, int age)
: base (name, age) {
}
}
class program {
static void main( string[] args) {
student xyy = new student( "小月月" , 12);
student fj = new student( "凤姐" , 14);
student fr = new student( "芙蓉姐姐" , 16);
student xl = new student( "犀利哥" , 18);
list <student > student = new list <student >();
student.add(xyy);
student.add(fj);
student.add(fr);
student.add(xl);
teacher tea = new teacher( "wanghao" , 21);
//student.add(tea);//因为list<student> 限制类型必须为student 所以不能添加teacher对象
//比arraylist更加限制存储的类型 而且效率要高 因为添加 取出对象是不会发生装箱 拆箱的操作的
//遍历
foreach (student item in student) {
console .writeline(item.name);//因为返回的就是student对象 所以可以直接读取属性值
console .writeline(item.age);
}
//移除
student.remove(xyy); //移除的是引用地址 所以下面移除的s不是xyy
student s = new student( "小月月" , 12);
student.remove(s);
student.removeat(0);
student.removerange(0, 2); //从0索引移除后面两位 即前移除前两位学生 xyy fj
//student.removeall();//移除所有满足条件的 详见帮助文档
student.clear();
console .writeline(student.contains(xyy));
//toarray()方法 转换学生类型数组
student [] stu = student.toarray();
foreach (student item in stu) {
console .writeline(item.name);
}
console .read();
}
}
}
b、dictonary<>字典
view code
using system;
using system.collections.generic;
namespace _03_泛型集合 {
class student {
public student(string name, int age) {
this .name = name;
this .age = age;
}
private string name;
public string name {
get {
return name;
}
set {
name = value ;
}
}
private int age;
public int age {
get {
return age;
}
set {
age = value ;
}
}
}
class program {
static void main( string[] args) {
student xyy = new student( "小月月" , 12);
student fj = new student( "凤姐" , 14);
student fr = new student( "芙蓉姐姐" , 16);
student xl = new student( "犀利哥" , 18);
dictionary <string , student> student = new dictionary < string, student >();//key为string类型的name value为student对象
student.add( "小月月" , xyy);
student.add( "凤姐" , fj);
student.add( "芙蓉姐姐" , fr);
student.add( "犀利哥" , xl);
console .writeline(student["犀利哥" ].name); //根据key获取value
//遍历 通过key
foreach (string item in student.keys) {
console .writeline(item);
console .writeline(student[item].age);
}
//遍历 通过value
foreach (student item in student.values) {
console .writeline(item.age);
}
//遍历键值对
foreach (keyvaluepair < string, student > item in student) {
console .writeline(item.key);
console .writeline(item.value.age);//item.value是student对象 直接使用
}
//移除
//student.remove("小月月");
//student.clear();
student.containskey( "小月月" ); //是否包含该key
//更多参见帮助文档
console .read();
}
}
}
c、泛型集合练习
view code
using system;
using system.collections.generic;
namespace _04__泛型练习 {
class program {
static void main( string[] args) {
//把分拣奇偶数的程序用泛型实现
string str = "7 4 3 2 9 8 33 22" ;
string [] strs = str.split(' ' );
strs = getevent(strs).toarray();
string res = string .join( " ", strs); //string数组 直接用join就好了
console .writeline(res);
//将int数组中的奇数放到一个新的int数组中返回
int [] intarr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
list <int > list = new list <int >();
foreach (int item in intarr) {
if (item % 2 != 0) {
list.add(item);
}
}
intarr = list.toarray();
foreach (int item in intarr) {
console .writeline(item);
}
//从一个整数的list<int>中取出最大数。不使用自身带的max()方法。
list <int > list2 = new list <int > { 1, 2, 3, 4, 5, 6, 7, 8 };
int max = list2[0];
foreach (int item in list2) {
if (item > max) {
max = item;
}
}
console .writeline("泛型集合最大值为{0}" , max);
console .readkey();
}
public static list< string > getevent(string [] str) {
list <string > list = new list <string >();
list <string > list2 = new list <string >();
foreach (string item in str) {
if (int .parse(item) % 2 != 0) {
list.add(item);
} else {
list2.add(item);
}
}
list.addrange(list2);
return list;
}
}
}
d、泛型集合练习2
view code
using system;
using system.collections.generic;
namespace _06_泛型集合练习 {
class program {
static void main( string[] args) {
//把1,2,3转换为壹贰叁
string str = "1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零" ;
dictionary <char , char> money = new dictionary < char, char >();
string [] strs = str.split(' ' );
string s = "123456789" ;
string news = "" ;
for (int i = 0; i < strs.length; i++) {
money.add(strs[i][0], strs[i][1]);
}
foreach (char item in s) {
news += money[item];
}
console .writeline(news);
char n = '1' ;
console .writeline(n + ' ' ); //结果显示为81 char字符相加是把其asic码相加的
console .writeline(n + 2);//结果显示为51
//如果想实现字符串相加就把任一个参数改变为tostring输出
console .writeline(n.tostring() + 2);//显示为12
//计算字符串中每种字符出现的次数(面试题)。“welcome to chinaworld”,不区分大小写,打印“w 2” “e 2” “o 3”……
string str2 = "welcome to chinaworld" ;
dictionary <char , int> num = new dictionary < char, int >();
foreach (char item in str2.tolower().replace( " " , "" )) {
if (num.containskey(item)) {
num[item]++;
} else {
num.add(item, 1);
}
}
foreach (var key in num.keys) {
console .writeline("\"{0}{1}\"" , key, num[key]);
}
//编写一个函数进行日期转换,将输入的中文日期转换为阿拉伯数字日期,比如:二零一二年十二月月二十一日要转换为2012-12-21。
string date = "二零一二年十二月二十一日" ; //date2的转换 需要考虑十左右字符是否都在字典中 在则为 则十消失 如果左边不在右边在则变1 如果左边在右边不在则变0 如果左右都不在则变10 还是蛮复杂的
//string date = "二零一二年二月二一日";
string strnumb = "一1 二2 三3 四4 五5 六6 七7 八8 九9 零0" ;
string [] strnumbs = strnumb.split(' ' );
string nullyear = "" ;
dictionary <char , char> years = new dictionary < char, char >();
for (int i = 0; i < strnumbs.length; i++) {
years.add(strnumbs[i][0], strnumbs[i][1]);
}
//years.add('年', '-');
//years.add('月', '-');
for (int i = 0; i < date.length; i++) {
if (years.containskey(date[i])) {
nullyear += years[date[i]];
} else if (date[i] == '年' || date[i] == '月' ) {
nullyear += '-' ;
} else if (date[i] == '十' && years.containskey(date[i + 1]) && !years.containskey(date[i - 1])) {
nullyear += '1' ;
} else if (date[i] == '十' && !years.containskey(date[i + 1]) && years.containskey(date[i - 1])) {
nullyear += '0' ;
} else if (date[i] == '十' && !years.containskey(date[i + 1]) && !years.containskey(date[i - 1])) {
nullyear += "10" ;
}
}
console .writeline(nullyear);
console .readkey();
}
}
}
e、泛型集合练习2中日期转换提取为方法
view code
using system;
using system.collections.generic;
namespace _07_日期change {
class program {
static string str = "一1 二2 三3 四4 五5 六6 七7 八8 九9 零0" ;
static string [] strs = str.split( ' ');
static dictionary < char, char > money = new dictionary< char , char >();
static void main( string[] args) {
for (int i = 0; i < strs.length; i++) {
money.add(strs[i][0], strs[i][1]);
}
//string date = "二零一二年二月二一日";
string date = "二零一二年二十月二十一日" ;
date = newstr(date);
string nullyear = "" ;
//money.add('年', '-');
//money.add('月', '-');
for (int i = 0; i < date.length; i++) {
if (money.containskey(date[i])) {
nullyear += money[date[i]];
} else if (date[i] == '年' || date[i] == '月' ) {
nullyear += '-' ;
}
}
console .writeline(nullyear);
console .writeline(date);//二零一二年一二月二一日
console .readkey();
}
//十左右字符都在字典中 那么十消失 如果左边不在右边在则变1 如果左边在右边不在则变0 如果左右都不在则变10
public static string newstr( string str) {
string newstr = "" ;
for (int i = 0; i < str.length; i++) {
if (str[i] == '十' ) {
bool left = money.containskey(str[i - 1]);
bool right = money.containskey(str[i + 1]);
if (left && right) {
newstr += "" ;
} else if (right) { //!left &&
newstr += "一" ;
} else if (left) { //&& !right
newstr += "零" ;
} else {//if (!left && !right)
newstr += "一零" ;
}
} else {
newstr += str[i];
}
}
return newstr;
}
}
}
f、泛型集合练习之翻译软件
view code
using system;
using system.collections.generic;
using system.io;
using system.linq;
using system.text;
using system.windows.forms;
namespace _05_翻译软件 {
public partial class form1 : form {
public form1() {
initializecomponent();
}
dictionary <string , string> dic = new dictionary < string, string >();
private void btnchange_click( object sender, eventargs e) {
if (dic.containskey(txtenglish.text)) {
txtchina.text = dic[txtenglish.text];
} else {
messagebox .show("请购买新的字典" );
}
}
private void form1_load( object sender, eventargs e) {
string [] filecon = file .readalllines( "英汉词典txt格式.txt" , encoding .default);//格式遵循abandon v.抛弃,放弃
for (int i = 0; i < filecon.count(); i++) {
string [] arr = filecon[i].split(new char[] { ' ' }, stringsplitoptions .removeemptyentries);
if (!dic.containskey(arr[0])) {
dic.add(arr[0], arr[1]);
}
}
}
}
}
复制代码 代码如下:
view code
using system;
using system.collections.generic;
namespace _02_泛型集合 {
class person {
public person(string name, int age) {
this .name = name;
this .age = age;
}
private string name;
public string name {
get {
return name;
}
set {
name = value ;
}
}
private int age;
public int age {
get {
return age;
}
set {
age = value ;
}
}
}
class student : person {
public student(string name, int age)
: base (name, age) {
}
}
class teacher : person {
public teacher(string name, int age)
: base (name, age) {
}
}
class program {
static void main( string[] args) {
student xyy = new student( "小月月" , 12);
student fj = new student( "凤姐" , 14);
student fr = new student( "芙蓉姐姐" , 16);
student xl = new student( "犀利哥" , 18);
list <student > student = new list <student >();
student.add(xyy);
student.add(fj);
student.add(fr);
student.add(xl);
teacher tea = new teacher( "wanghao" , 21);
//student.add(tea);//因为list<student> 限制类型必须为student 所以不能添加teacher对象
//比arraylist更加限制存储的类型 而且效率要高 因为添加 取出对象是不会发生装箱 拆箱的操作的
//遍历
foreach (student item in student) {
console .writeline(item.name);//因为返回的就是student对象 所以可以直接读取属性值
console .writeline(item.age);
}
//移除
student.remove(xyy); //移除的是引用地址 所以下面移除的s不是xyy
student s = new student( "小月月" , 12);
student.remove(s);
student.removeat(0);
student.removerange(0, 2); //从0索引移除后面两位 即前移除前两位学生 xyy fj
//student.removeall();//移除所有满足条件的 详见帮助文档
student.clear();
console .writeline(student.contains(xyy));
//toarray()方法 转换学生类型数组
student [] stu = student.toarray();
foreach (student item in stu) {
console .writeline(item.name);
}
console .read();
}
}
}
b、dictonary<>字典
复制代码 代码如下:
view code
using system;
using system.collections.generic;
namespace _03_泛型集合 {
class student {
public student(string name, int age) {
this .name = name;
this .age = age;
}
private string name;
public string name {
get {
return name;
}
set {
name = value ;
}
}
private int age;
public int age {
get {
return age;
}
set {
age = value ;
}
}
}
class program {
static void main( string[] args) {
student xyy = new student( "小月月" , 12);
student fj = new student( "凤姐" , 14);
student fr = new student( "芙蓉姐姐" , 16);
student xl = new student( "犀利哥" , 18);
dictionary <string , student> student = new dictionary < string, student >();//key为string类型的name value为student对象
student.add( "小月月" , xyy);
student.add( "凤姐" , fj);
student.add( "芙蓉姐姐" , fr);
student.add( "犀利哥" , xl);
console .writeline(student["犀利哥" ].name); //根据key获取value
//遍历 通过key
foreach (string item in student.keys) {
console .writeline(item);
console .writeline(student[item].age);
}
//遍历 通过value
foreach (student item in student.values) {
console .writeline(item.age);
}
//遍历键值对
foreach (keyvaluepair < string, student > item in student) {
console .writeline(item.key);
console .writeline(item.value.age);//item.value是student对象 直接使用
}
//移除
//student.remove("小月月");
//student.clear();
student.containskey( "小月月" ); //是否包含该key
//更多参见帮助文档
console .read();
}
}
}
c、泛型集合练习
复制代码 代码如下:
view code
using system;
using system.collections.generic;
namespace _04__泛型练习 {
class program {
static void main( string[] args) {
//把分拣奇偶数的程序用泛型实现
string str = "7 4 3 2 9 8 33 22" ;
string [] strs = str.split(' ' );
strs = getevent(strs).toarray();
string res = string .join( " ", strs); //string数组 直接用join就好了
console .writeline(res);
//将int数组中的奇数放到一个新的int数组中返回
int [] intarr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
list <int > list = new list <int >();
foreach (int item in intarr) {
if (item % 2 != 0) {
list.add(item);
}
}
intarr = list.toarray();
foreach (int item in intarr) {
console .writeline(item);
}
//从一个整数的list<int>中取出最大数。不使用自身带的max()方法。
list <int > list2 = new list <int > { 1, 2, 3, 4, 5, 6, 7, 8 };
int max = list2[0];
foreach (int item in list2) {
if (item > max) {
max = item;
}
}
console .writeline("泛型集合最大值为{0}" , max);
console .readkey();
}
public static list< string > getevent(string [] str) {
list <string > list = new list <string >();
list <string > list2 = new list <string >();
foreach (string item in str) {
if (int .parse(item) % 2 != 0) {
list.add(item);
} else {
list2.add(item);
}
}
list.addrange(list2);
return list;
}
}
}
d、泛型集合练习2
复制代码 代码如下:
view code
using system;
using system.collections.generic;
namespace _06_泛型集合练习 {
class program {
static void main( string[] args) {
//把1,2,3转换为壹贰叁
string str = "1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零" ;
dictionary <char , char> money = new dictionary < char, char >();
string [] strs = str.split(' ' );
string s = "123456789" ;
string news = "" ;
for (int i = 0; i < strs.length; i++) {
money.add(strs[i][0], strs[i][1]);
}
foreach (char item in s) {
news += money[item];
}
console .writeline(news);
char n = '1' ;
console .writeline(n + ' ' ); //结果显示为81 char字符相加是把其asic码相加的
console .writeline(n + 2);//结果显示为51
//如果想实现字符串相加就把任一个参数改变为tostring输出
console .writeline(n.tostring() + 2);//显示为12
//计算字符串中每种字符出现的次数(面试题)。“welcome to chinaworld”,不区分大小写,打印“w 2” “e 2” “o 3”……
string str2 = "welcome to chinaworld" ;
dictionary <char , int> num = new dictionary < char, int >();
foreach (char item in str2.tolower().replace( " " , "" )) {
if (num.containskey(item)) {
num[item]++;
} else {
num.add(item, 1);
}
}
foreach (var key in num.keys) {
console .writeline("\"{0}{1}\"" , key, num[key]);
}
//编写一个函数进行日期转换,将输入的中文日期转换为阿拉伯数字日期,比如:二零一二年十二月月二十一日要转换为2012-12-21。
string date = "二零一二年十二月二十一日" ; //date2的转换 需要考虑十左右字符是否都在字典中 在则为 则十消失 如果左边不在右边在则变1 如果左边在右边不在则变0 如果左右都不在则变10 还是蛮复杂的
//string date = "二零一二年二月二一日";
string strnumb = "一1 二2 三3 四4 五5 六6 七7 八8 九9 零0" ;
string [] strnumbs = strnumb.split(' ' );
string nullyear = "" ;
dictionary <char , char> years = new dictionary < char, char >();
for (int i = 0; i < strnumbs.length; i++) {
years.add(strnumbs[i][0], strnumbs[i][1]);
}
//years.add('年', '-');
//years.add('月', '-');
for (int i = 0; i < date.length; i++) {
if (years.containskey(date[i])) {
nullyear += years[date[i]];
} else if (date[i] == '年' || date[i] == '月' ) {
nullyear += '-' ;
} else if (date[i] == '十' && years.containskey(date[i + 1]) && !years.containskey(date[i - 1])) {
nullyear += '1' ;
} else if (date[i] == '十' && !years.containskey(date[i + 1]) && years.containskey(date[i - 1])) {
nullyear += '0' ;
} else if (date[i] == '十' && !years.containskey(date[i + 1]) && !years.containskey(date[i - 1])) {
nullyear += "10" ;
}
}
console .writeline(nullyear);
console .readkey();
}
}
}
e、泛型集合练习2中日期转换提取为方法
复制代码 代码如下:
view code
using system;
using system.collections.generic;
namespace _07_日期change {
class program {
static string str = "一1 二2 三3 四4 五5 六6 七7 八8 九9 零0" ;
static string [] strs = str.split( ' ');
static dictionary < char, char > money = new dictionary< char , char >();
static void main( string[] args) {
for (int i = 0; i < strs.length; i++) {
money.add(strs[i][0], strs[i][1]);
}
//string date = "二零一二年二月二一日";
string date = "二零一二年二十月二十一日" ;
date = newstr(date);
string nullyear = "" ;
//money.add('年', '-');
//money.add('月', '-');
for (int i = 0; i < date.length; i++) {
if (money.containskey(date[i])) {
nullyear += money[date[i]];
} else if (date[i] == '年' || date[i] == '月' ) {
nullyear += '-' ;
}
}
console .writeline(nullyear);
console .writeline(date);//二零一二年一二月二一日
console .readkey();
}
//十左右字符都在字典中 那么十消失 如果左边不在右边在则变1 如果左边在右边不在则变0 如果左右都不在则变10
public static string newstr( string str) {
string newstr = "" ;
for (int i = 0; i < str.length; i++) {
if (str[i] == '十' ) {
bool left = money.containskey(str[i - 1]);
bool right = money.containskey(str[i + 1]);
if (left && right) {
newstr += "" ;
} else if (right) { //!left &&
newstr += "一" ;
} else if (left) { //&& !right
newstr += "零" ;
} else {//if (!left && !right)
newstr += "一零" ;
}
} else {
newstr += str[i];
}
}
return newstr;
}
}
}
f、泛型集合练习之翻译软件
复制代码 代码如下:
view code
using system;
using system.collections.generic;
using system.io;
using system.linq;
using system.text;
using system.windows.forms;
namespace _05_翻译软件 {
public partial class form1 : form {
public form1() {
initializecomponent();
}
dictionary <string , string> dic = new dictionary < string, string >();
private void btnchange_click( object sender, eventargs e) {
if (dic.containskey(txtenglish.text)) {
txtchina.text = dic[txtenglish.text];
} else {
messagebox .show("请购买新的字典" );
}
}
private void form1_load( object sender, eventargs e) {
string [] filecon = file .readalllines( "英汉词典txt格式.txt" , encoding .default);//格式遵循abandon v.抛弃,放弃
for (int i = 0; i < filecon.count(); i++) {
string [] arr = filecon[i].split(new char[] { ' ' }, stringsplitoptions .removeemptyentries);
if (!dic.containskey(arr[0])) {
dic.add(arr[0], arr[1]);
}
}
}
}
}
下一篇: 手把手教你用Android自定义饼状图