zoukankan      html  css  js  c++  java
  • 论泛型

    泛型:顾名思义泛泛的数据类型,也就是不确定的意思。

    泛型集合:对所保存的元素进行类型约束,在调用添加值类型时无需拆箱装箱,避免了许多不必要的操作!

    常用的泛型集合:List<T>和Dictionary<K,V>。

    ADO.NET中我们可以通过编写SQL语句来对程序中的数据进行增.删.改.查等等操作,那么泛型集合中的元素可以进行增.删.改.查等操作吗?

    一:List<T>

    当然可以

    增:

    使用集合的Add()方法

    方法一:

     1  Person p1 = new Person();
     2             p1.name = "张三";
     3             p1.id = "20090101";
     4             p1.sex = "";
     5             p1.age=20;
     6             Person p2 = new Person();
     7             p2.name = "李四";
     8             p2.id = "20090102";
     9             p2.sex = "";
    10             p2.age = 20;
    11             list.Add(p1);
    12             list.Add(p2);

    方法二:使用构造函数赋值

     1 class Person
     2 {
     3         //构造函数
     4         public Person(string grade01, int earth03, string fight01)
     5         {
     6             grade = grade01;
     7             earth = earth03;
     8             fight = fight01;
     9         }

    10 }

      

     1     static void Main(string[] args)
     2         {
     3              Person p2 = new Person("90级", 500000, "8999");
     4             list.Add(p2);
     5             foreach (Person item in list)
     6             {
     7                 Console.WriteLine("巫师的生命值是:{0},战斗力是:{1},       等级是{2}", item.Earth, item.Fight, item.Grade);
     8             }
     9             Console.ReadLine();
    10 }

    删:

    使用Remove()和RemoveAt()方法。

    使用foreach遍历集合,找到满足条件的一项进行删除

     1   string id=this.dataGridView1.SelectedRows[0].Cells["id"].Value.ToString();
     2                     foreach (Person item in list)
     3                     {
     4                         if(item.id==id)
     5                         {
     6                             list.Remove(item);
     7                             this.dataGridView1.DataSource = new BindingList<Person>(list);
     8                             break;
     9                         }
    10                     }

    改:同样使用foreach遍历集合,找到满足条件的一项进行修改。

     1  for (int i = 0; i < frm.list.Count; i++)
     2                 {
     3                     if (frm.list[i].name == this.name)
     4                     {
     5                         frm.list[i].name = txtname.Text;
     6                         frm.list[i].id = txtid.Text;
     7                         frm.list[i].age = Convert.ToInt32(txtage.Text);
     8                         frm.list[i].sex = this.cobsex.SelectedItem.ToString();
     9                     }
    10                 }

    二:Dictionary<K,V>

    首先确定好Key和Value的数据类型

    增:

    1 HealthCheckSet he = new HealthCheckSet();
    2         this.item.Add(textBox1.Text, he);

    删:

    使用Remove()方法,删除带有特定键的元素。

     1  static void Main(string[] args)
     2         {
     3             //先添加一个元素
     4             //删除一个元素
     5             Dictionary<string, Person> dic = new Dictionary<string, Person>();
     6             Person p1 = new Person();
     7             p1.name = "张三";
     8             p1.age = 19;
     9             dic.Add(p1.name, p1);
    10             dic.Remove(p1.name);
    11             foreach (KeyValuePair<string,Person> item in dic)
    12             {
    13                 Console.WriteLine("key是:{0},Value是:{1}", item.Key, item.Value.age);
    14             }
    15             Console.ReadLine();
    16         }

    改:

     1      static void Main(string[] args)
     2         {
     3              4             //将年龄从19改为20
     5             Dictionary<string, Person> dic = new Dictionary<string, Person>();
     6             Person p1 = new Person();
     7             p1.name = "张三";
     8             p1.age = 19;
     9             dic.Add(p1.name, p1);
    10             foreach (Person item in dic.Values)
    11             {
    12                 if (item.age == 19)
    13                 {
    14                     item.age = 20;
    15                 }
    16                 Console.WriteLine("年龄是:{0}", item.age);
    17             }
    18             Console.ReadLine();
    19         }

    三:泛型类

    1  public class Cat<R>
    2     {
    3         public R Name { get; set; }
    4     }
     1  Cat<string> cat=new Cat<string>();
     2             cat.Name = "2";
     3 
     4 
     5             Cat<int> cat2 = new Cat<int>();
     6             cat2.Name = 12;
     7 
     8             //数组和集合都是    数据类型
     9             Cat<List<string>> cat3 = new Cat<List<string>>();
    10             List<string> list=new List<string>();
    11             cat3.Name =list;
    12 
    13 
    14             Cat<Dictionary<string,string>> cat4=new Cat<Dictionary<string, string>>();
    15             Dictionary<string,string> dic=new Dictionary<string, string>();
    16             cat4.Name = dic;

     

  • 相关阅读:
    hdu 5387 Clock (模拟)
    CodeForces 300B Coach (并查集)
    hdu 3342 Legal or Not(拓扑排序)
    hdu 3853 LOOPS(概率DP)
    hdu 3076 ssworld VS DDD(概率dp)
    csu 1120 病毒(LICS 最长公共上升子序列)
    csu 1110 RMQ with Shifts (线段树单点更新)
    poj 1458 Common Subsequence(最大公共子序列)
    poj 2456 Aggressive cows (二分)
    HDU 1869 六度分离(floyd)
  • 原文地址:https://www.cnblogs.com/chimingyang/p/5363730.html
Copyright © 2011-2022 走看看