第三章 使用集合组织相关数据

一、集合的概念:

       集合:某些指定的对象集在一起就是集合。

       比如:一个容器内有机器猫,樱桃小丸子,奥特曼,这个包括他们的容器就叫做集合

       数组:可以存储相同数据类型的一堆数据的容器

       自动扩容:Capcity和Count区别?

       Capacity:集合占用空间

       Count:集合存储元素个数

二、ArrayList的使用:

      

       在S1的时候我们学习了数组,但是数组有一定的局限性,由于给数组进行添加,删除的时候特别麻烦

所以有了集合来帮忙实现所具备的功能而不像数组那么局限

       集合的优点:{1.自动扩容

                  {2.集合中很多方法能让我们更加快捷的操作集合中的数据

       集合的本质:{1.使用ArrayList首先需要引入命名空间

                  {2.集合动态扩容原理:如果集合中元素个数为0,并没有开辟空间

                  {3.默认如果集合中出现第一个元素,那么集合的大小为4,如果放入第5个元素那么扩容为8

                  (即:初始4,出现5的时候4*2,此时初始为8,出现9即8*2,依次循环。。。)

                  int num=(this.item.Length==0)?4:(this.Length*2);

       函数:给类定义一个方法,方法名和类名相同没有返回值,没有void

              public class Student

              {  

                     public Student()

                     {

                           

                     }

              }

      

  命名空间:

     同名类便于区分,加快检索硬盘上某个类的速度

     Using System.Collections

     Using System.Collections.Generic; 泛型命名,默认导入

      

       删除:

       {1.Remove

       {2.RemoveAt

       {3.Clear

三、HashTable(哈希表)

       根据键(Key)可以查找到相应的值 (Value)

       三种方法:{1.通过key找value  key和value都显示

                 foreach (var item in table.Keys)

                {2.只要获取value

                  foreach (var item in table.Values)

                {3.同时遍历key和value

                  foreach (DictionaryEntry item in table)

DictionayEntry是一个结构,定义可设置或者检索Hashtable的键/值

       删除的两种方法:

            Remove() 

            HashTable没有RemoveAt()方法,因为根本没有索引

            table.Remove("001");

            table.Clear();

      

  var :隐式类型推断

      比如:var num=5; 那么var=int  即: var name="小明"; 那么var=string 

四、泛型

泛型:

       就是为了约束ArrayList中元素类型,而制定的一个新的集合类型,该类型只能加入同一类型的多个元素,标识符<T>,可以看成是一个占位符,泛型是将运行时错误提前到了编译时

List<T>无需类型转换

Dictionary<K,V>:具有泛型的全部特性

Dictionary<string,person> dic=new Dictionary<string,person>

泛型类(不常用)

一道练习题:员工打卡(签到和签退)

  实现思路及关键代码:

  

    1.创建Record类,记录签到和签退信息     

public class Record
{
public DateTime register { get; set; }//签到时间
public DateTime reghoer { get; set; }//签退时间
public string ID { get; set; }//工号
public string Name { get; set; }//姓名
}

    2.用Dicitionary<string,Record>保存员工打卡记录,其中的键(keys)为员工(ID)

 public Dictionary<string, Record> recordList = new Dictionary<string, Record>();//保存员工打卡记录

    3.选择“签到”必须索引员工打卡记录,如果签到,提示不能再签到

//签到
private void register_Click(object sender, EventArgs e)
{
//确保有选中的行
if (this.myDgv.SelectedRows.Count!=1)
{
MessageBox.Show("请选择一行数据!","操作提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
//确保没有签到过
string worok = myDgv.CurrentRow.Cells["Clnid"].Value.ToString();
foreach (string item in recordList.Keys)
{
if (worok == item)
{
MessageBox.Show("抱歉您已经签到过了!OK");
return;
}
}
//执行签到
Record cord = new Record();
cord.ID = worok;
cord.Name = myDgv.CurrentRow.Cells["Clnname"].Value.ToString();
cord.register = DateTime.Now;
this.recordList.Add(cord.ID, cord);
MessageBox.Show("签到成功了哦★");
}
}

    4.选择“签退”,必须索引员工打卡记录,如果未签到,不允许签退操作

/// <summary>
/// 签退
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void reghoer_Click(object sender, EventArgs e)
{
//确保有选中行
if (myDgv.SelectedRows.Count != 1)
{
MessageBox.Show("抱歉您没有选中一行数据");
return;
}
else
{
string ID = myDgv.CurrentRow.Cells["Clnid"].Value.ToString();
bool bol = false;
foreach (string item in recordList.Keys)
{
if (item == ID)
{
recordList[item].reghoer = DateTime.Now;
MessageBox.Show("签退成功!");
bol = true;
return;
}
}
if (!bol)
{
MessageBox.Show("抱歉未签到过!");
}
}
}

*泛型集合Dictionary的基本操作

*使用泛型集合绑定DataGridView

  

原文地址:https://www.cnblogs.com/gaoweixiao99/p/4620132.html