A, list<> generic collection
Copy Code code as follows:
View Code
Using System;
Using System.Collections.Generic;
Namespace _02_ Generic Collection {
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 ("Xiao Yue Yue", 12);
Student FJ = new Student ("Sister Feng", 14);
Student FR = new Student ("Sister Furong", 16);
Student XL = new Student ("Brother Sharp", 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> limit type must be Student
More restrictive storage types than ArrayList and efficient because a boxed unboxing operation does not occur when you add a Fetch object.
Traverse
foreach (Student item in Student) {
Console. WriteLine (item. Name);//Because the student object is returned, the property value can be read directly
Console. WriteLine (item. Age);
}
Removed from
Student. Remove (XYY); The removed is the reference address so the following removed s are not XYY
Student s = new Student ("Xiao Yue Yue", 12);
Student. Remove (s);
Student. RemoveAt (0);
Student. RemoveRange (0, 2); Remove the back two bits from the 0 index the first two students XYY FJ
Student. RemoveAll ()//Remove all details of the help document that meet the criteria
Student. Clear ();
Console. WriteLine (student. Contains (XYY));
ToArray () method converts an array of student types
Student [] stu = Student. ToArray ();
foreach (Student item in Stu) {
Console. WriteLine (item. Name);
}
Console. Read ();
}
}
}
B, Dictonary<> Dictionary
Copy Code code as follows:
View Code
Using System;
Using System.Collections.Generic;
Namespace _03_ Generic Collection {
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 ("Xiao Yue Yue", 12);
Student FJ = new Student ("Sister Feng", 14);
Student FR = new Student ("Sister Furong", 16);
Student XL = new Student ("Brother Sharp", 18);
Dictionary <string, student> Student = new Dictionary < string, Student > ();//key for string type name value is Student Object
Student. Add ("Xiao Yue Yue", XYY);
Student. Add ("Sister Feng", FJ);
Student. Add ("Sister Furong", FR);
Student. Add ("Brother Sharp", XL);
Console. WriteLine (student["Brother Sharp"). Name); Get value based on key
Traversal through key
foreach (String item in student. Keys) {
Console. WriteLine (item);
Console. WriteLine (Student[item]. Age);
}
Traversal through value
foreach (Student item in Student. Values) {
Console. WriteLine (item. Age);
}
Traversal key value pairs
foreach (KeyValuePair < string, Student > item in Student) {
Console. WriteLine (item. Key);
Console. WriteLine (item. Value.age);//item. Value is used directly by the student object
}
Removed from
Student. Remove ("Xiao Yue Month");
Student. Clear ();
Student. ContainsKey ("Xiao Yue Yue"); Whether to include the key
More See Help documentation
Console. Read ();
}
}
}
C, generic collection exercises
Copy Code code as follows:
View Code
Using System;
Using System.Collections.Generic;
Namespace _04__ generics Exercise {
Class Program {
static void Main (string[] args) {
The process of sorting odd-even numbers is implemented using generics
String str = "7 4 3 2 9 8 33 22";
string [] STRs = str. Split (")";
STRs = Getevent (STRs). ToArray ();
string res = string. Join ("", STRs); String array directly with the join IS good
Console. WriteLine (RES);
Returns the odd number in an int array to a new int array
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);
}
Remove the maximum number from the list<int> of an integer. The Max () method that does not use its own band.
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 ("Generic collection maximum 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 collection Exercise 2
Copy Code code as follows:
View Code
Using System;
Using System.Collections.Generic;
namespace _06_ Generic Collection Practice {
Class Program {
static void Main (string[] args) {
Convert 1,2,3 to one or three
String str = "1 One 2 3 4 5 Wu 6 lu 7 qi 8 ba 9 nine 00";
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 that the addition of the eight Char character is added to the ASIC code.
Console. WriteLine (n + 2);//Results shown as 51
If you want to implement a string addition, change either parameter to the ToString output
Console. WriteLine (n.tostring () + 2);//displayed as 12
Calculates the number of occurrences of each character in a string (interview question). "Welcome to Chinaworld", case-insensitive, printing "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]);
}
Write a function for date conversion, convert the input Chinese date to Arabic numeral date, for example: December 2012 month 21st to convert to 2012-12-21.
String date = "December 21, 2012"; Date2 conversion needs to consider whether 10 or so characters are in the dictionary, then 10 disappear if the left side is not on the right, then the 1 if the left is not on the right, then change 0 if it's not around then change 10 or it's quite complicated.
String date = "February 21, 2012";
String strnumb = "1.,122,334,455,667,79e,+19";
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] = = ' Ten ' && years. ContainsKey (Date[i + 1]) &&!years. ContainsKey (Date[i-1])) {
Nullyear + = ' 1 ';
else if (date[i] = = ' Ten ' &&!years. ContainsKey (Date[i + 1]) && years. ContainsKey (Date[i-1])) {
Nullyear + = ' 0 ';
else if (date[i] = = ' Ten ' &&!years. ContainsKey (Date[i + 1]) &&!years. ContainsKey (Date[i-1])) {
Nullyear + = "10";
}
}
Console. WriteLine (nullyear);
Console. Readkey ();
}
}
}
E, the method of date conversion extraction in generic set Exercise 2
Copy Code code as follows:
View Code
Using System;
Using System.Collections.Generic;
Namespace _07_ Date Change {
Class Program {
static string str = "1.,122,334,455,667,79e,+19";
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 = "February 21, 2012";
String date = "Ishina 21st";
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)/December 2012 21st
Console. Readkey ();
}
About 10 characters are in the dictionary then 10 disappears if the left side is not on the right, then the 1 if the left is not on the right, then change 0.
public static string Newstr (String str) {
String newstr = "";
for (int i = 0; i < str. Length; i++) {
if (str[i] = = ' Ten ') {
bool left = money. ContainsKey (Str[i-1]);
bool right = money. ContainsKey (Str[i + 1]);
if (left && right) {
Newstr + = "";
else if (right) {//!left &&
Newstr = "one";
else if (left) {//&&!right
Newstr + = "0";
else {//if (!left &&!right)
Newstr + = "10";
}
} else {
Newstr + = Str[i];
}
}
return newstr;
}
}
}
F, the translation software of generic set practice
Copy Code code 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 buy a new dictionary");
}
}
private void Form1_Load (object sender, EventArgs e) {
string [] Filecon = File. ReadAllLines ("TXT format of English-Chinese dictionary), Encoding." Default);//format follows abandon v. Discard, discard
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]);
}
}
}
}
}