初识 C#/.net 基础【02】

  • ArrayList 集合
      ArrayList arrayList = new ArrayList();
      arrayList.Add("添加一条内容");
      arrayList.Add("再添加一条内容");
      arrayList.Add(6666666);
      arrayList.Insert(1, "这个里面是添加插入的内容");// 参数1:插入的位置
      arrayList.RemoveAt(3);// 参数:移除索引处的内容
      arrayList.Remove("再添加一条内容");// 参数:填写需要移除的内容
  • ArrayList 的劣势
  • ArrayList 在存储数据时使用 object 类型进行存储的
  • ArrayList 不是类型安全的,使用时很可能会出现类型不匹配的错误
  • 就算都有插入了同一类型的数据,但在使用的时候,我们也需要将它们转化为对应的原类型来处理
  • ArrayList 的存储存在 装箱和拆箱 操作,导致其性能低下
  • 【父类可以接受子类的数据】
  • 装箱:就是将比如 int 类型或者其他类型的对象通过 隐式转换 赋值给 object对象
  • 拆箱:就是将 object 对象通过 显示转换 赋值给int或者其他类型的变量
  • 装箱与拆箱的过程会产生较多的性能损耗

  • List 集合
      List<int> intList = new List<int>();
      intList.Add(123);// 定义了 int 类型的泛型限制,List集合无法存放其他的类型
      intList[0] = 666;// 索引的范围必须在集合长度的范围内【修改内容】
      intList.Insert(0, 999);// 参数1:插入的位置----参数2:插入的内容
      intList.RemoveAt(0);// 参数:移除索引处的内容
      intList.Remove(666);// 参数:填写需要移除的内容

      List<int> intList2 = new List<int>()
      {
        1, 2, 3, 4, 5, 6
      };
      intList2.Clear();// 清空集合中的内容
      
      /*
       * 在 List 集合中插入不对的数据类型就会报错,比如上面的的集合中插入 string 类型的数据系统就会提示错误
       * 这样就避免了 ArrayList 中的类型安全问题与装箱拆箱的性能问题
       * Lsit 泛型的好处指通过允许指定 泛型类或方法 操作的 特定的类型 ,
       * 减少了类型强制转换的需要和运行时错误的可能性,泛型提供了类型安全,但是没有增加开销
       */
  • List 的指定类型也可以是 特定的类型 就如下面的实例
      List<ClassYin02> people = new List<ClassYin02>();
      ClassYin02 person2 = new ClassYin02()
      {
        Name = "小印01",
        Age = 22,
        Height = 173
      };
      people.Add(person2);

      people.Add(new ClassYin02() { 
        Name = "小印02",
        Age = 23,
        Height = 175
      });
      ClassYin02 person2_1 = people[1];

      people.RemoveAt(0);// 这样可以成功移除
      people.Remove(person2);// 这样可以成功移除【在 Add 的时候是 person2,这里删除的也是 person2】
      people.Remove(person2_1);

      // 下面是不可以成功删除的
      ClassYin02 person2_2 = new ClassYin02()
      {
        Name = "小印03",
        Age = 24,
        Height = 180
      };
      people.Add(new ClassYin02 { 
        Name = "小印03",
        Age = 24,
        Height = 180
      });
      ClassYin02 person2_3 = people[0];
      people.Remove(person2_2);// 这样不可以成功删除【因为后面 Add 进去的重新 new 的新地址,前面的 person2_2 的地址与现在这个地址不一样】
      people.Remove(person2_3);// 这样就是可以成功删除的【person2_3 = people[0],这样 person2_3 的地址就是和 people[0] 的地址相同】

      /*
       * Remove() 这个里面识别的是“地址”,不是相同的内容
       * 所以即使 person2_2 和后面 Add 进去的内容一样,也不能删除 people 里面与之内容相同的
       * 【上面所述的“地址”不是同一个地址空间,但是存在一定的指向关系】
       * 【上面所述 new 的新地址,是切断了 person2_2 与 ClassYin02() 的某种关联】
       */
  • ClassYin02 类
  class ClassYin02 {
    public string Name { get; set; }
    public int Age { get; set; }
    public int Height { get; set; }
  }

  • Dictionary<> 字典
      // 方式一:Add 方法赋值
      Dictionary<string, string> dictionary01 = new Dictionary<string, string>();
      dictionary01.Add("小印01", "眼睛很大");// 参数1:键 ---- 参数2:值
      dictionary01.Add("小印02", "体型标准");
      dictionary01.Add("小印03", "性格很好");
      // dictionary01.Add("小印03", "性格很好呀");// 像这样的 键值 已经存在的,不能进行添加,运行后会报错

      // 方法二:通过键赋值
      dictionary01["小印01"] = "被修改啦";// 左边 [] 内的参数是 键,右边的参数是与之相对应的值
      dictionary01["小印04"] = "我是新人";

      // 方法三:对象初始化器
      Dictionary<int, string> dictionary02 = new Dictionary<int, string> {
        {0001, "药品01" },
        {0002, "药品02" },
        {0003, "药品03" }
      };

      // 赋值扩展
      Dictionary<string, List<string>> huBei = new Dictionary<string, List<string>>();
      List<string> huBeiList = new List<string>();
      huBei.Add("湖北", huBeiList);
      huBeiList.Add("武汉");
      huBeiList.Add("荆州");
      huBeiList.Add("仙桃");
      List<string> jingZhouiList = new List<string>();
      huBei.Add("荆州", jingZhouiList);
      jingZhouiList.Add("峰口");
      jingZhouiList.Add("新题");
      jingZhouiList.Add("白庙");
      jingZhouiList.Add("万全");

      /*
       * 键与值可以是任何类型
       * 键:在设置时是唯一的
       * 值:只能对应唯一的键,键可以有多个值
       */

      // 取字典中的值
      String value01 = dictionary01["小印01"];
      // 移除对应的键值对
      dictionary01.Remove("小印01");

  • foreach 循环
      List<int> intList00 = new List<int>() { 
      1,2,4,5,6,10
      };
      foreach (int i in intList00)
      {
        // 每次循环,i 都是 intList00 集合中的一个元素【数组的操作就是把此例中的集合改成数组】
        Console.WriteLine(i);
      }

      // foreach 遍历取值【操作字典】
      foreach (KeyValuePair<string, List<string>> i in huBei)
      {
        // i.Key 取的是键 ---- i.Value 取的是值
        Console.WriteLine(i.Key);
        foreach (string j in i.Value)
        {
          Console.WriteLine("    " + j);
        }
      }
  • foreach 扩展
      ClassYin02_1 xinXi = new ClassYin02_1();
      List<ClassYin02_1> xinXiList = xinXi.GetXinXi();
      foreach (ClassYin02_1 xx in xinXiList)
      {
        Console.WriteLine(xx.Name + "的年龄是" + xx.Age + ",身高是" + xx.Height);
      }
  • ClassYin02_1 类
  public class ClassYin02_1 {
    public string Name { get; set; }
    public int Age { get; set; }
    public int Height { get; set; }
    public List<ClassYin02_1> GetXinXi()
    {
      ClassYin02_1 xinXi01 = new ClassYin02_1();
      xinXi01.Name = "小印01";
      xinXi01.Age = 22;
      xinXi01.Height = 173;
      ClassYin02_1 xinXi02 = new ClassYin02_1();
      xinXi02.Name = "小印02";
      xinXi02.Age = 23;
      xinXi02.Height = 178;
      ClassYin02_1 xinXi03 = new ClassYin02_1();
      xinXi03.Name = "小印03";
      xinXi03.Age = 24;
      xinXi03.Height = 180;

      List<ClassYin02_1> xinXiList = new List<ClassYin02_1>();
      xinXiList.Add(xinXi01);
      xinXiList.Add(xinXi02);
      xinXiList.Add(xinXi03);
      return xinXiList;
    }
  }

笔记源码


一点点笔记,以便以后翻阅。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小印丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值