• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
    問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
    當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

    集合類(lèi)List與Dictonary實(shí)例練習(xí)

    來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:41:36
    文檔

    集合類(lèi)List與Dictonary實(shí)例練習(xí)

    集合類(lèi)List與Dictonary實(shí)例練習(xí):a、List<>泛型集合 代碼如下:View Code using System; using System.Collections.Generic; namespace _02_泛型集合 { class Person { public Person(string name, int age) { this .name = nam
    推薦度:
    導(dǎo)讀集合類(lèi)List與Dictonary實(shí)例練習(xí):a、List<>泛型集合 代碼如下:View Code using System; using System.Collections.Generic; namespace _02_泛型集合 { class Person { public Person(string name, int age) { this .name = nam

    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);//因?yàn)長(zhǎng)ist<Student> 限制類(lèi)型必須為Student 所以不能添加Teacher對(duì)象
    //比ArrayList更加限制存儲(chǔ)的類(lèi)型 而且效率要高 因?yàn)樘砑?取出對(duì)象是不會(huì)發(fā)生裝箱 拆箱的操作的
    //遍歷
    foreach (Student item in student) {
    Console .WriteLine(item.Name);//因?yàn)榉祷氐木褪莝tudent對(duì)象 所以可以直接讀取屬性值
    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索引移除后面兩位 即前移除前兩位學(xué)生 xyy fj
    //student.RemoveAll();//移除所有滿(mǎn)足條件的 詳見(jiàn)幫助文檔
    student.Clear();
    Console .WriteLine(student.Contains(xyy));
    //ToArray()方法 轉(zhuǎn)換學(xué)生類(lèi)型數(shù)組
    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類(lèi)型的name value為Student對(duì)象
    student.Add( "小月月" , xyy);
    student.Add( "鳳姐" , fj);
    student.Add( "芙蓉姐姐" , fr);
    student.Add( "犀利哥" , xl);
    Console .WriteLine(student["犀利哥" ].Name); //根據(jù)key獲取value
    //遍歷 通過(guò)key
    foreach (string item in student.Keys) {
    Console .WriteLine(item);
    Console .WriteLine(student[item].Age);
    }
    //遍歷 通過(guò)value
    foreach (Student item in student.Values) {
    Console .WriteLine(item.Age);
    }
    //遍歷鍵值對(duì)
    foreach (KeyValuePair < string, Student > item in student) {
    Console .WriteLine(item.Key);
    Console .WriteLine(item.Value.Age);//item.Value是Student對(duì)象 直接使用
    }
    //移除
    //student.Remove("小月月");
    //student.Clear();
    student.ContainsKey( "小月月" ); //是否包含該key
    //更多參見(jiàn)幫助文檔
    Console .Read();
    }
    }
    }

    c、泛型集合練習(xí)
    代碼如下:

    View Code
    using System;
    using System.Collections.Generic;
    namespace _04__泛型練習(xí) {
    class Program {
    static void Main( string[] args) {
    //把分揀奇偶數(shù)的程序用泛型實(shí)現(xiàn)
    string str = "7 4 3 2 9 8 33 22" ;
    string [] strs = str.Split(' ' );
    strs = Getevent(strs).ToArray();
    string res = string .Join( " ", strs); //string數(shù)組 直接用join就好了
    Console .WriteLine(res);
    //將int數(shù)組中的奇數(shù)放到一個(gè)新的int數(shù)組中返回
    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);
    }
    //從一個(gè)整數(shù)的List<int>中取出最大數(shù)。不使用自身帶的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、泛型集合練習(xí)2
    代碼如下:

    View Code
    using System;
    using System.Collections.Generic;
    namespace _06_泛型集合練習(xí) {
    class Program {
    static void Main( string[] args) {
    //把1,2,3轉(zhuǎn)換為壹貳叁
    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 + ' ' ); //結(jié)果顯示為81 char字符相加是把其Asic碼相加的
    Console .WriteLine(n + 2);//結(jié)果顯示為51
    //如果想實(shí)現(xiàn)字符串相加就把任一個(gè)參數(shù)改變?yōu)閠ostring
    輸出
    Console .WriteLine(n.ToString() + 2);//顯示為12
    //計(jì)算字符串中每種字符出現(xiàn)的次數(shù)(面試題)。“Welcome to Chinaworld”,不區(qū)分大小寫(xiě),打印“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]);
    }
    //編寫(xiě)一個(gè)函數(shù)進(jìn)行日期轉(zhuǎn)換,將輸入的中文日期轉(zhuǎn)換為阿拉伯?dāng)?shù)字日期,比如:二零一二年十二月月二十一日要轉(zhuǎn)換為2012-12-21。
    string date = "二零一二年十二月二十一日" ; //date2的轉(zhuǎn)換 需要考慮十左右字符是否都在字典中 在則為 則十消失 如果左邊不在右邊在則變1 如果左邊在右邊不在則變0 如果左右都不在則變10 還是蠻復(fù)雜的
    //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、泛型集合練習(xí)2中日期轉(zhuǎn)換提取為方法
    代碼如下:

    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、泛型集合練習(xí)之翻譯軟件
    代碼如下:

    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("請(qǐng)購(gòu)買(mǎi)新的字典" );
    }
    }
    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]);
    }
    }
    }
    }
    }

    聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    集合類(lèi)List與Dictonary實(shí)例練習(xí)

    集合類(lèi)List與Dictonary實(shí)例練習(xí):a、List<>泛型集合 代碼如下:View Code using System; using System.Collections.Generic; namespace _02_泛型集合 { class Person { public Person(string name, int age) { this .name = nam
    推薦度:
    標(biāo)簽: 類(lèi)型 集合 練習(xí)
    • 熱門(mén)焦點(diǎn)

    最新推薦

    猜你喜歡

    熱門(mén)推薦

    專(zhuān)題
    Top
    主站蜘蛛池模板: 久久国产乱子伦精品免费强| 国产精品无码v在线观看| 国产成人亚洲精品青草天美| 国产69精品久久久久99尤物| 国产精品无圣光一区二区| 亚洲国产精品一区二区三区久久 | 日本精品久久久中文字幕| 日韩精品无码人妻一区二区三区| 久久国产亚洲精品麻豆| 欧美精品一区二区蜜臀亚洲| 欧美成人精品网站播放| 99热日韩这里只有精品| 精品久久久久久中文字幕| 精品久久久噜噜噜久久久| 亚洲欧洲精品成人久久曰影片| 精品乱子伦一区二区三区高清免费播放 | 一本精品中文字幕在线| 国产欧美日本精品| 91精品国产综合久久香蕉| 国产精品久久久久久搜索| 人妻AV一区二区三区精品 | 国产偷伦精品视频| 四虎最新永久在线精品免费| 97久久综合精品久久久综合 | 亚洲欧美日韩国产精品| 欧美日韩精品久久久免费观看| 国产精品 码ls字幕影视| 精品一区二区久久久久久久网站| 99精品久久精品| 97精品国产一区二区三区| 国产精品免费AV片在线观看| 久久精品国产亚洲AV高清热 | 91精品成人免费国产| 精品国产第一国产综合精品| 久久久久人妻精品一区二区三区| 色一乱一伦一图一区二区精品 | 国产在线精品一区二区不卡麻豆| 999精品色在线播放| 亚洲国产精品线在线观看| 亚洲综合国产精品| 国产91精品黄网在线观看|