zoukankan      html  css  js  c++  java
  • 有关c#的学习笔记整理与心得

    【 塔 · 第 一 条 约 定 】

    整理c#:Array Arraylist List Hashtable Dictionary Stack Queue等

    1. Array 的容量是固定的,而 ArrayList 的容量是根据需要自动扩展的。ArrayList 提供添加、插入或移除某一范围元素的方法。
    2. ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处:
      动态的增加和减少元素
      实现了ICollection和IList接口
      灵活的设置数组的大小
      eg:(array)
    • 在声明数组的同时给其分配空间:
      数组类型[] 数组名=new 数组类型[数组长度]
      -声明数组,分配空间和赋值一同完成
      数组类型[]数组名={值1,值2,值3}
      例如:int[]arr={1,2,3}等价于int[]arr=new int[]{1,2,3}
      eg:(arraylist)
    • ArrayList arrl = new ArrayList() 个数不用指定
    • arrl.Add('')
       arrl.Remove('')
       arrl.RemoveAt('')
       arrl.RemoveRange(1, 3) 移除一到三号元素
       arrl.Sort() 升序
       arrl.Reverse() 反转
       arrl.Clear() 清空
    1. List
      eg:(List)
    • List mList = new List() 其中T为声明的种类
      T为列表中元素类型,现在以string类型作为例子:
    List<string> mList = new List<string>();
    
    • List testList =new List (IEnumerable collection) 以一个集合为参数创建List
    string[] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu"};
    
      List<string> testList = new List<string>(temArr);
    
    • List. Add(T item)添加一个元素
    • List. AddRange(IEnumerable collection)添加一组元素
    string[] temArr = {"Ha","Hunter","Tom","Lily","Jay","Jim","Kuku","Locu"};mList.AddRange(temArr);
    
    • Insert(intindex, T item);在index位置添加一个元素
    mList.Insert(1,"Hei");
    
    • 判断某个元素是否在该List中:

      List. Contains(T item)返回true或false
      

    if(mList.Contains("Hunter"))
    
      {
    
      Console.WriteLine("There is Hunter in the list");
    
      }
    
      else
    
      {
    
      mList.Add("Hunter");
    
      Console.WriteLine("Add Hunter successfully.");
    
      }
    

    (List与arraylist差不多)
    4. 哈希表(Hashtable)
    eg:

    • 命名空间:System.Collections
    • Hashtable hshTable = new Hashtable() 创建哈希表
    • hshTable .Add("Person1", "zhanghf") 往哈希表里添加键值对
    • hshTable .Clear() 移除哈希表里所有的键值对
    • hshTable .Contains("Person1") 判断哈希表里是否包含该键
    • string name = (string)hshTable["Person1"].ToString() 取哈希表里指定键的值
    • hshTable.Remove("Person1") 删除哈希表里指定键的键值对
    • IDictionaryEnumerator en = hshTable.GetEnumerator() 遍历哈希表所有的键,读出相应的值
      (也和那几个具体用法差不多)
    1. (Dictionary)
    • 结构:Dictionary<[key], [value]>
    • 他的特点是存入对象是需要与[key]值一一对应的存入该泛型
      通过某一个一定的[key]去找到对应的值
      eg:
    //实例化对象
    Dictionary<int, string> dic = new Dictionary<int, string>();
    //对象打点添加
    dic.Add(1, "one");
    dic.Add(2, "two");
    dic.Add(3, "one");
    //提取元素的方法
    string a = dic[1];
    string b = dic[2];
    string c = dic[3];
    //1、2、3是键,分别对应“one”“two”“one”
    //上面代码中分别把值赋给了a,b,c
    
    1. 堆栈(Stack)
    • 堆栈(Stack)代表了一个后进先出的对象集合。当您需要对各项进行后进先出的访问时,则使用堆栈。当您在列表中添加一项,称为推入元素,当您从列表中移除一项时,称为弹出元素。
    • Count 获取 Stack 中包含的元素个数。
    • public virtual void Clear();
      从 Stack 中移除所有的元素。
    • public virtual bool Contains( object obj );
      判断某个元素是否在 Stack 中。
    • public virtual object Peek();
      返回在 Stack 的顶部的对象,但不移除它。
    • public virtual object Pop();
      移除并返回在 Stack 的顶部的对象。
    • public virtual void Push( object obj );
      向 Stack 的顶部添加一个对象。
    • public virtual object[] ToArray();
      复制 Stack 到一个新的数组中。
      eg:
    Stack st = new Stack();
    
                st.Push('A');
                st.Push('M');
                st.Push('G');
                st.Push('W');
    
    1. 队列(Queue)
    • 队列(Queue)代表了一个先进先出的对象集合。当您需要对各项进行先进先出的访问时,则使用队列。当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队。
      (与堆栈差不多)

    整理C#:for、foreach、while、switch等

    1. for
      和c差不多
    2. foreach:
      foreach 语句为数组或对象集合中的每个元素重复一个嵌入语句组。foreach 语句用于循环访问集合以获取所需信息,但不应用于更改集合内容以避免产生不可预知的副作用。
    • C#中:foreach 针对引用类型地址的访问
      如果当前方法中在访问当前引用类型的集合,在新调用的 方法中在修改当前集合的地址时,当前地址会依然存在,不符合C#中的对象的生命周期,一旦当前文件的引用地址被替换掉了,当前对象的空间就消失废弃了,但是,foreach(){
      }会保留原先的集合的地址<地址应该也是有生命周期的>foreach 和for 就不一样!
    • 语法格式如下:

    foreach(type identifier in expression)
    {
    embedded-statement
    }

    type(类型)和identifier(标识符)用于声明循环变量,expression(表达式)对应集合。

     int[]arr=newint[]{0,1,2,3,4};
    foreach(int i in arr)
    {
    Console.Write(i);
    }
    
    1. while
    int i=0;
      while(i<arr.Length)
      {
        Console.Write(arr[i] + " ");
        i++;
      }
      Console.WriteLine();
    
    1. switch
      (和c差不多)
      借了个栗子:
    Console.Write("请输入分数(整数): ");
      int score_in = Convert.ToInt32(Console.ReadLine());
      if (score_in < 0) score_in = -100;//防止-9到-1被归为不及格
      switch(score_in/10)
      {
        case 10:
        case 9: Console.WriteLine("优秀"); break;
        case 8: Console.WriteLine("良好"); break;
        case 7: 
        case 6: Console.WriteLine("及格"); break;
        case 5:
        case 4:
        case 3:
        case 2:
        case 1:
        case 0: Console.WriteLine("不及格"); break;
        default: Console.WriteLine("输入错误!"); break;
      }
    
  • 相关阅读:
    FreeCommander 学习手册
    String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1)
    StringBuffer 详解 (String系列之3)
    StringBuilder 详解 (String系列之2)
    java io系列26之 RandomAccessFile
    java io系列25之 PrintWriter (字符打印输出流)
    java io系列24之 BufferedWriter(字符缓冲输出流)
    java io系列23之 BufferedReader(字符缓冲输入流)
    java io系列22之 FileReader和FileWriter
    java io系列21之 InputStreamReader和OutputStreamWriter
  • 原文地址:https://www.cnblogs.com/mercuialC/p/6380204.html
Copyright © 2011-2022 走看看