집합 클래스 List 와 Dictonary 인 스 턴 스 연습

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]);
}
}
}
}
}

좋은 웹페이지 즐겨찾기