A. List <> generic set
Copy codeThe Code is as follows: View Code
Using System;
Using System. Collections. Generic;
Namespace _ 02 _ generic set {
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 ("Fengjie", 14 );
Student fr = new Student ("Sister Furong", 16 );
Student xl = new Student ("sharp brother", 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); // The Teacher object cannot be added because the List <Student> restriction type must be Student.
// The storage type is more restricted than the ArrayList and the efficiency is high, because adding and removing objects does not cause packing and unpacking.
// Traverse
Foreach (Student item in student ){
Console. WriteLine (item. Name); // the property value can be directly read because the returned result is a student object.
Console. WriteLine (item. Age );
}
// Remove
Student. Remove (xyy); // The referenced address is removed, so the s removed below is not xyy
Student s = new Student ("", 12 );
Student. Remove (s );
Student. RemoveAt (0 );
Student. RemoveRange (0, 2); // remove the last two from the 0 index, that is, remove the first two students xyy fj.
// Student. RemoveAll (); // remove all the items that meet the conditions. For details, see the help documentation.
Student. Clear ();
Console. WriteLine (student. Contains (xyy ));
// ToArray () method to convert the student type array
Student [] stu = student. ToArray ();
Foreach (Student item in stu ){
Console. WriteLine (item. Name );
}
Console. Read ();
}
}
}
B. Dictonary <> dictionaryCopy codeThe Code is as follows: View Code
Using System;
Using System. Collections. Generic;
Namespace _ 03 _ generic set {
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 ("Fengjie", 14 );
Student fr = new Student ("Sister Furong", 16 );
Student xl = new Student ("sharp brother", 18 );
Dictionary <string, Student> student = new Dictionary <string, Student> (); // The name value of the key type string is the Student object.
Student. Add ("", xyy );
Student. Add ("Fengjie", fj );
Student. Add ("Sister Furong", fr );
Student. Add ("sharp brother", xl );
Console. WriteLine (student ["sharp brother"]. Name); // obtain the value based on the key
// Traverse key
Foreach (string item in student. Keys ){
Console. WriteLine (item );
Console. WriteLine (student [item]. Age );
}
// Traverse through value
Foreach (Student item in student. Values ){
Console. WriteLine (item. Age );
}
// Traverse Key-value pairs
Foreach (KeyValuePair <string, Student> item in student ){
Console. WriteLine (item. Key );
Console. WriteLine (item. Value. Age); // item. Value is directly used by the Student object.
}
// Remove
// Student. Remove (" ");
// Student. Clear ();
Student. ContainsKey (""); // whether the key is included
// For more information, see the help documentation.
Console. Read ();
}
}
}
C. Generic set exercisesCopy codeThe Code is as follows: View Code
Using System;
Using System. Collections. Generic;
Namespace _ 04 _ generic exercises {
Class Program {
Static void Main (string [] args ){
// Implement the Program for sorting the parity using the generic type
String str = "7 4 3 2 9 8 33 22 ";
String [] strs = str. Split ('');
Strs = Getevent (strs). ToArray ();
String res = string. Join ("", strs); // The string array can be directly joined.
Console. WriteLine (res );
// Place the odd number in the int array into a new int array and return
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 );
}
// Obtain the maximum number from an integer List <int>. Do not use its own Max () method.
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 ("the maximum value of a generic set is {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. Generic set exercise 2Copy codeThe Code is as follows: View Code
Using System;
Using System. Collections. Generic;
Namespace _ 06 _ generic set exercise {
Class Program {
Static void Main (string [] args ){
// Converts 1, 2, 3 to 1, 3
String str = "1, 2, 2, 3, 4, 4, 5, 5, 6, land, 7, 8, 9, 0, 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 + ''); // The result is displayed as 81 char characters, which is used to add the Asic code.
Console. WriteLine (n + 2); // The result is 51.
// If you want to add strings, change any parameter to tostring output.
Console. WriteLine (n. ToString () + 2); // displayed as 12
// Calculate the number of times each character appears in the string (interview question ). "Welcome to Chinaworld", case-insensitive. Print "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]);
}
// Compile a function to convert the input Chinese date to an Arabic digit date. For example, the date must be converted from January 1, December 2012 to January 21, 2012-12-21.
String date = "December 21, 2012 "; // For date2 conversion, you need to consider whether or not the 10 or so characters are in the dictionary. If the left is not on the right, the value is changed to 1. If the left is not on the right, the value is changed to 0. if not, it would be complicated to change to 10.
// String date = "August 1, February 2, 2012 ";
String strNumb = "one 1 2 3 3 4 5 6 6 7 8 9 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 ('Year ','-');
// Years. Add ('month ','-');
For (int I = 0; I <date. Length; I ++ ){
If (years. ContainsKey (date [I]) {
NullYear + = years [date [I];
} Else if (date [I] = 'Year' | date [I] = 'month '){
NullYear + = '-';
} Else if (date [I] = '10' & years. ContainsKey (date [I + 1]) &! Years. ContainsKey (date [I-1]) {
NullYear + = '1 ';
} Else if (date [I] = '10 '&&! Years. ContainsKey (date [I + 1]) & years. ContainsKey (date [I-1]) {
NullYear + = '0 ';
} Else if (date [I] = '10 '&&! Years. ContainsKey (date [I + 1]) &! Years. ContainsKey (date [I-1]) {
NullYear + = "10 ";
}
}
Console. WriteLine (nullYear );
Console. ReadKey ();
}
}
}
E. Extraction of date conversion in generic set exercise 2 as a methodCopy codeThe Code is as follows: View Code
Using System;
Using System. Collections. Generic;
Namespace _ 07 _ date Change {
Class Program {
Static string str = "1, 2, 3, 4, 5, 6, 7, 8, 9, 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 = "August 1, February 2, 2012 ";
String date = "August 20-August 21 ";
Date = newstr (date );
String nullYear = "";
// Money. Add ('Year ','-');
// Money. Add ('month ','-');
For (int I = 0; I <date. Length; I ++ ){
If (money. ContainsKey (date [I]) {
NullYear + = money [date [I];
} Else if (date [I] = 'Year' | date [I] = 'month '){
NullYear + = '-';
}
}
Console. WriteLine (nullYear );
Console. WriteLine (date); // August 1, 2012-August 1, February 2
Console. ReadKey ();
}
// 10 or so characters are in the dictionary. If the left is not on the right, 1 is changed. If the left is not on the right, 0 is changed. If the left is not on the right, 10 is changed.
Public static string newstr (string str ){
String newstr = "";
For (int I = 0; I <str. Length; I ++ ){
If (str [I] = '10 '){
Bool left = money. ContainsKey (str [I-1]);
Bool right = money. ContainsKey (str [I + 1]);
If (left & right ){
Newstr + = "";
} Else if (right ){//! Left &&
Newstr + = "1 ";
} Else if (left ){//&&! Right
Newstr + = "zero ";
} Else {// if (! Left &&! Right)
Newstr + = "One Zero ";
}
} Else {
Newstr + = str [I];
}
}
Return newstr;
}
}
}
F. translation software for generic set exercisesCopy codeThe Code is as follows: View Code
Using System;
Using System. Collections. Generic;
Using System. IO;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Namespace _ 05 _ translation software {
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 ("please purchase a new dictionary ");
}
}
Private void Form1_Load (object sender, EventArgs e ){
String [] filecon = File. ReadAllLines ("English dictionary TXT", Encoding. Default); // The format follows abandon v. Discard and give up
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]);
}
}
}
}
}