zoukankan      html  css  js  c++  java
  • 塔 · 第 一 条 约 定

    C#之数组:

    相比较于C语言可以说说数组在定义声明的语法存在一些不同,在C#中的语法是这样的:
    数据类型[] 数组名称 = new 数据类型[元素个数] //而这里字符串类型是string 不是char;
    例如 int[] a=new int[10];
    

    举个冒泡排序的例子:

    int[] a = { 9,8,7,6,5,4,3,2,1,0};//直接赋值
                int i,j,temp;
                for (i = 0; i < a.Length-1 ; i++)//a.Length是数组元素个数;
                 for(j = 0 ;j < a.Length-1-i ; j++)
                {
                    if(a[j]>a[j+1])
                        {
                            temp = a[j];
                            a[j] = a[j + 1];
                            a[j + 1] = temp;
                        }
                }
                for(i=0;i<a.Length;i++)
                {
                    Console.Write(a[i] + " ");
                }
                Console.ReadLine();
    

    C#ArryList:

    指的是动态数组,可以对数组元素进行一些添加,删除,复制等等的功能,需要用到using system.collection
    语法:Arrcy 数组名称=new Arraylist(); //括号中可以写数组名
    代码例子:
    
    int[] a = { 9,8,7,6,5,4,3,2,1,0};//直接赋值
                int i,j,temp;
                ArrayList list = new ArrayList(a);//初始化动态数组;
                list.Add("a");//在末尾添加一个数组元素,也可添加数组名
                list.Add("49");//加不加引号没有关系
                list.Insert(1, "aa");//在第一个数组元素后面插入"aa"
                ArrayList list2 = new ArrayList();//此时的list2是没有赋值的,一开始为null
                list2.Add("tttt");//添加元素
                list2.Add("xxx");
                list.InsertRange(2, list2);//在第二个数组元素后面插入一个数组
                //list.Remove(5);//删除元素中的5
                //list.RemoveAt(5);//删除第九个元素
                Console.WriteLine(list.Count);//list.count 为动态数组list的长度
                for(i=0;i<list.Count;i++)
                {
                    Console.Write(list[i] + " ");
                    //输出结果为 9 aa tttt xxx 8 7 6 5 4 3 2 1 0 a
                }
                Console.WriteLine(" ");
                Console.WriteLine(a.Length);
                for(i=0;i<a.Length;i++)
                {
                    Console.Write(a[i] + " ");//使用动态数组对原数组无影响
                } 
    

    -从网上查到还有好多功能,例如查询,删除所以元素,排序等;
    -a.RemoveRange(1,3);//是删除aList动态数组中a[1]到a[3]的元素;
    -查找:a=aList.IndexOf(查找的元素);//找到,则返回查找到的元素个数
    b=aList.IndexOf(查找的元素);//没找到,返回-1
    (arrylist 部分就写这么多了,例子大部分在代码里)

    C#之List:

    List的用法与ArrayList类似,用百度上的说法:需要System.Collections.GenericList<T>类是ArrayList类的泛型等效类。该类使用大小可按需动态增加的数组实现。
    泛型的好处:它为使用c#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类型转换,所以性能得到提高。
    声明:List<数据类型> 名称 = new List<数据类型>();//与动态数组类似,括号中存放数组
    对于数组元素的删除,添加,判断元素存在的语法像动态数组的语法一样,同样的a.add(),a.remove(),a.contains();其中a.contains( );返回true或者false。
    
                string[] a = { "a", "c", "b", "g", "e", "d" };
                List<string> mlist = new List<string>(a);
                mlist.Add("f");//在末尾添加元素
                mlist.Insert(2, "x");//插入添加元素
                for (int i = 0; i < mlist.Count; i++)
                {
                    Console.Write(mlist[i] + " ");
                }
                Console.WriteLine();
                mlist.Sort();//排序(升序);
                for(int i=0;i<mlist.Count;i++)
                {
                    Console.Write(mlist[i] + " ");
                }
                Console.WriteLine();
                mlist.Reverse();//顺序反转
                for (int i = 0; i < mlist.Count; i++)
                {
                    Console.Write(mlist[i] + " ");
                }
                Console.WriteLine();
                //mlist.Clear();//元素清空
                int n=mlist.Count(); //计算元素个数
                //mlist.AddRanger(数组名称);//添加一组元素
                //list.find的运用:
                string c = mlist.Find(b =>
                 {
                     if (b == "e")//b=="y"
                         return true;
                     else
                         return false;
                 });
                Console.WriteLine(c);//输出e
                /*在整个大括号里,是元素查询条件,若符合则返回true,否则false
                 *最后输出c 来判断其中是否存在符合条件的元素,若不符合则不输出,按顺序找到首个符合条件的元素就结束*/
                 //对于list.FindLast是相反逆序,方法与以上类似
    

    还有其他函数:
    -List.TrueForAll: 确定是否List中的每个元素都与指定的谓词所定义的条件相匹配。
    -语法:bool flag = list.TrueForAll(.... ) 与find函数类似
    -List.FindAll:检索与指定谓词所定义的条件相匹配的所有元素。
    -语法:List list2 = list.FindAll(ListFind(函数));
    -ListFind函数:

    public 
    bool ListFind(string name) 
            {  
                if (name.Length > 3)//条件 
                {  
                    return true; 
                }  
                return false; 
            }
    
    -List.Take(n):获得前n行 返回值为IEnumetable<T>,T的类型与List<T>的类型一样。
    -语法:IEnumerable<T> list3 = list.Take(n)//list3存放list的前n个元素。
    -List.Where:与List.FindAll相似
    -语法:IEnumerable<string> whereList = mList.Where(...);
    -List.RemoveAll :移除符合条件的元素
    -语法:list.RemoveAll(....);
    

    C#之Hashtable:

    使用到的命名空间:System.Collections
    作用:用于处理和表现类似keyvalue的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中keyvalue键值对均为object类型,所以Hashtable可以支持任何类型的keyvalue键值对. (根据百度结果)  T.T
     声明:Hashtable table = new Hashtable(); //与以上集合类似
    

    这里Hashtable 是成对出现的有key和value相互对应;内涵可以有很多种
    例子:

        using System;
        using System.Collections; //file使用Hashtable时,必须引入这个命名空间
        class Program
        {
          public static void Main()
          {
             Hashtable ht = new Hashtable(); //创建一个Hashtable实例
             ht.Add("北京", "帝都"); //添加keyvalue键值对
             ht.Add("上海", "魔都");
             ht.Add("广州", "省会");
             ht.Add("深圳", "特区");
             string capital = (string)ht["北京"];
             Console.WriteLine(ht.Contains("上海")); //判断哈希表是否包含特定键,其返回值为true或false
             ht.Remove("深圳"); //移除一个keyvalue键值对
             ht.Clear(); //移除所有元素
          }
        }
    

    可以使用不同的数据类型不一定要字符串对字符串的key和value
    可以有两种方法遍历哈希表 按照key或者value 使用时的表明方法 : table.Keys or table.Values;
    如果想要更详细,请点击来自大佬的博客
    这个整理不下去了T.T

    C#之Dictionary:

    感觉这个和之前的哈希表很像,声明:Dictionary<数据类型,数据类型> 名称 = new Dictionary<数据类型,数据类型>();//前后需对应,也对应key和value
    

    C#之Stack:

    经过前面的几种类型,可以看出它们都是同一性质的东西(鄙人不才)
    

    懒到直接截图了。
    例子:

    Stack st = new Stack();
    
                st.Push('A');//添加
                st.Push('M');
                st.Push('G');
                st.Push('W');
                
                Console.WriteLine("Current stack: ");
                foreach (char c in st)//遍历st
                {
                    Console.Write(c + " ");
                }
                Console.WriteLine();
                
                st.Push('V');
                st.Push('H');
                Console.WriteLine("The next poppable value in stack: {0}", 
                st.Peek());//返回到首个元素,移动到首个元素的位置
                Console.WriteLine("Current stack: ");           
                foreach (char c in st)
                {
                   Console.Write(c + " ");
                }
                Console.WriteLine();
    
                Console.WriteLine("Removing values ");
                st.Pop();//移除首个元素
                st.Pop();
                st.Pop();
                
                Console.WriteLine("Current stack: ");
                foreach (char c in st)
                {
                   Console.Write(c + " "); 
                }
    


    (已吐出一口老血)

    C#之Queue:

    这个可以说与stack类似但是存储方式不同(在计算机导论中说过)
    Queue:
    ![](http://images2015.cnblogs.com/blog/1092267/201702/1092267-20170206031555026-862368419.png)
    区别
    Stack:
    ![](http://images2015.cnblogs.com/blog/1092267/201702/1092267-20170206031612557-1062472902.png)
    语法:
    ![](http://images2015.cnblogs.com/blog/1092267/201702/1092267-20170206031818760-872077280.png)
    例子就不写了。。。
    

    详细的知识有实例

    C#之for、foreach、while、switch:

    C#中for、while、switch与C语言中无多大差别。
    C#foreach:在整理集合时从网上都可以了解到相关的foreach语句,主要时用与遍历数组。
    语法:foreach(数据类型 名称1 in 名称2) {.... }名称1是在大括号里使用,名称二是来自数组的名称。例如
    
     int[] s = new int[10];
                int i;
                for(i=0;i<10;i++)
                {
                    s[i] = i;
                }
                foreach(int m in s)
                {
                    Console.Write(m + " ");
                }
    


    以上的归纳,如果有错,望路过大佬、大牛指出来,谢谢!

  • 相关阅读:
    python的各版本的不同
    keras中的early stopping
    NER的数据处理
    ner处理数据的方式
    python的数据处理一
    linux下的终端利器----tmux
    BiseNet学习笔记
    《Harnessing Synthesized Abstraction Images to Improve Facial Attribute Recognition》论文阅读笔记
    转:玩玩三维重建
    《Cascaded Pyramid Network for Multi-Person Pose Estimation》论文阅读及复现笔记
  • 原文地址:https://www.cnblogs.com/q1076452761/p/6368758.html
Copyright © 2011-2022 走看看