zoukankan      html  css  js  c++  java
  • C# 集合汇总

      1 using System;
      2 using System.Collections;
      3 using System.Collections.Generic;
      4 using System.Linq;
      5 using System.Text;
      6 using System.Threading.Tasks;
      7 
      8 namespace ConsoleDemo.List
      9 {
     10 
     11     /// <summary>
     12     ///  基础回顾:集合:
     13     ///  1:线性结构  一对一关系
     14     ///  2:树形结构  一对多
     15     ///  3:图状结构  多对多
     16     /// </summary>
     17     public class ListDemo
     18     {
     19         public static void Get()
     20         {
     21             #region 线性结构
     22             {
     23 
     24                 {
     25                     /*
     26                      线性结构1:
     27                      缺陷: 长度要指定,同理string[]
     28                      优点:内存连续存储 节约空间,可以索引访问,读取速度快,增删慢                    
     29                      */
     30                     int[] list = new int[10];
     31                     list[0] = 5;
     32                     list[1] = 2;
     33                     list[2] = 0;
     34 
     35                 }
     36                 {
     37                     /*
     38                         线性结构1:
     39                         缺陷: 将 int[],string[]转化为泛型,装箱拆箱增加性能损耗
     40                         优点:内存连续存储 节约空间,可以索引访问,读取速度快,增删慢                    
     41                     */
     42                     ArrayList list = new ArrayList();
     43                     list.Add("sun");
     44                     list.Add("say");
     45                     list.Add("hello");
     46                     list.Add(DateTime.Now);
     47                 }
     48             }
     49             #endregion
     50 
     51             #region 链表结构
     52             {
     53                 /*
     54                  链表结构
     55                  单链表,双向链表,循环链表
     56                  存储格式:数据+地址
     57                  缺点:读取慢,增加了存储空间
     58                  优点:增删快
     59                  */
     60                 List<string> list = new List<string>();//使用泛型过程中制定了格式
     61                 list.Add("hello");
     62                 list.Add("shang hai");
     63             }
     64 
     65             {
     66                 //先进先出
     67                 Queue<string> list = new Queue<string>();
     68                 list.Enqueue("are"); //入队
     69                 list.Enqueue("you");
     70                 list.Enqueue("ok");
     71 
     72                 Console.WriteLine(string.Join(",", list));  //are,you,ok
     73 
     74                 list.Dequeue(); //出队
     75                 Console.WriteLine(string.Join(",", list));  //you,ok
     76 
     77                 //获取队列头部元素,不做移除动作
     78                 string value = list.Peek();//you
     79                 Console.WriteLine(value);
     80 
     81                 string value1 = list.Peek();//you
     82                 Console.WriteLine(value1);
     83             }
     84             {
     85                 //
     86                 Stack<string> list = new Stack<string>();
     87                 list.Push("易烊千玺");
     88                 list.Push("崇拜");
     89                 list.Push("");
     90                 Console.WriteLine(string.Join(",", list));  //我,崇拜,易烊千玺
     91 
     92                 list.Pop();
     93                 Console.WriteLine(string.Join(",", list));  //崇拜,易烊千玺
     94 
     95                 string value = list.Peek();
     96                 Console.WriteLine(value);  //崇拜
     97 
     98                 //支持重复
     99                 list.Push("崇拜");
    100                 Console.WriteLine(string.Join(",", list));  //崇拜,崇拜,易烊千玺
    101 
    102             }
    103 
    104             {
    105                 //排重,唯一性,IP投票 统计用户id等
    106                 HashSet<string> list = new HashSet<string>();
    107                 list.Add("");
    108                 list.Add("");
    109                 list.Add("");
    110                 list.Add("我看到明星了!");
    111                 //HashSet:啊,我看到明星了!   长度:2
    112                 Console.WriteLine("HashSet:" + string.Join(",", list) + "   长度:" + list.Count);
    113 
    114             }
    115             {
    116                 //排重,唯一性,IP投票 统计用户id等
    117                 SortedSet<string> list = new SortedSet<string>();
    118                 list.Add("");
    119                 list.Add("");
    120                 list.Add("");
    121                 list.Add("我看到明星了!");
    122 
    123                 //SortedSet: 啊,我看到明星了!   长度: 2
    124                 Console.WriteLine("SortedSet:" + string.Join(",", list) + "   长度:" + list.Count);
    125             }
    126             {
    127                 //自动排序
    128                 SortedSet<int> list = new SortedSet<int>();
    129                 list.Add(11);
    130                 list.Add(11);
    131                 list.Add(9);
    132                 list.Add(20);
    133 
    134                 //SortedSet:我期待的结果是自动排序了:9,11,20   长度: 3
    135                 Console.WriteLine("SortedSet:我期待的结果是自动排序了:" + string.Join(",", list) + "   长度:" + list.Count);
    136             }
    137             {
    138                 //增删都快的,用空间换性能
    139                 Hashtable list = new Hashtable();
    140 
    141                 list.Add("name", "sun");
    142                 list.Add("age", 18);
    143                 //list.Add("age", 19);//新增相同key会报错
    144 
    145                 Console.WriteLine(string.Join(",", list.Keys.Count)); //2
    146 
    147                 list.Remove("age");
    148                 Console.WriteLine(string.Join(",", list.Keys.Count));//1
    149 
    150                 bool IsExistName = list.Contains("name");
    151                 bool IsExistNa = list.Contains("na");
    152                 //True-False
    153                 Console.WriteLine(IsExistName + "-" + IsExistNa);
    154             }
    155             #endregion
    156 
    157 
    158             {
    159                 //IEnumerable 使用的时候linq to object方式
    160                
    161                 Console.WriteLine("_________________________");
    162                 MyColor colors = new MyColor();
    163                 foreach (string c in colors)
    164                 {
    165                     Console.WriteLine("color is : " + c);
    166                 }
    167                   
    168                 //ABC[] list = new ABC[10];
    169                 //list.Add
    170                 //foreach(var a in list)
    171                 //{
    172                 //    Console.WriteLine("ABC is :"+ a.Name); 
    173                 //}
    174 
    175 
    176                 int[] myArray = { 1, 32, 43, 343 };
    177                 IEnumerator myie = myArray.GetEnumerator();
    178                 myie.Reset();
    179                 while (myie.MoveNext())
    180                 {
    181                     int i = (int)myie.Current;
    182                     Console.WriteLine("Value: {0}", i);
    183                 }
    184 
    185 
    186                 /*
    187               
    188                 
    189                  延时执行: IQueryable,IEnumberalb 为延时执行(用到的时候再查),IList一次性加载
    190                  顺时执行: IList一次性查询后加载到内存
    191                  IQueryable接口与IEnumberable接口的区别:  
    192                  IEnumerable<T> 泛型类在调用自己的SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了,
    193                  IQueryable<T>  是将Skip ,take 这些方法表达式翻译成T-SQL语句之后再向SQL服务器发送命令,
    194                                 它并不是把所有数据都加载到内存里来才进行条件过滤。
    195 
    196 
    197                  */
    198 
    199 
    200                 //IQueryable 生成sql 采用表达式目录树,二叉树查找
    201                 //IQeurable(IQuerable<T>):不在内存加载持久数据,因为这家伙只是在组装SQL,(延迟执行) 到你要使用的时候,
    202                 //例如 list.Tolist() or list.Count()的时候,数据才从数据库进行加载(AsQueryable())。
    203                 //IQueryable<CustomData> list2 = dbcontext.CustomDataList.Where(t => t.PrimaryDataID == "123");
    204 
    205                 //IEnumberalb,使用的是LINQ to Object方式  内置委托,它会将AsEnumerable()时对应的所有记录都先加载到内存
    206                 //,然后在此基础上再执行后来的Query
    207                 //IEnumerable<CustomData> list2 = dbcontext.CustomDataList.Where(t => t.PrimaryDataID == "123").AsEnumerable();
    208 
    209 
    210                 // IList<CustomData> list2 = dbcontext.CustomDataList.Where(t => t.PrimaryDataID == "123").ToList();
    211 
    212 
    213 
    214                 //List:IList:ICollection:IEnumberable
    215                 Console.WriteLine("_________________________");
    216             }
    217         }
    218     }
    219     public class ABC
    220     {
    221         public string Name { get; set; }
    222     }
    223 
    224     /// <summary>
    225     /// 实现了IEnumberable接口也可以
    226     /// </summary>
    227     public class MyColor : IEnumerable
    228     {
    229         string[] colors = { "red", "white", "black", "yellow" };
    230         public IEnumerator GetEnumerator()
    231         {
    232             // throw new NotImplementedException();
    233             return colors.GetEnumerator();
    234         }
    235     }
    236 }
  • 相关阅读:
    Nginx使用
    nginx常见配置详解
    配置yum源
    nginx常见使用方式和日志功能
    SpringCloud学习篇《一》
    myeclipse的各种背景:黑色,护眼,欢迎围观
    java基础二 <流程控制语句, 方法,数组,java内存结构> 未完待续...
    fastjson解析超长json串以及转成list,map等方法实例
    Linux下权限的修改-JDK的配置-文件的常见操作
    java面试基础大全,绝对经典<126-170><转>
  • 原文地址:https://www.cnblogs.com/hanliping/p/11116772.html
Copyright © 2011-2022 走看看