zoukankan      html  css  js  c++  java
  • C#数组 List、Dictionary 、Arrary、ArrayList

     #region Dictionary  泛型集合,动态修改查询、查询和排序
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("北京", "1");
                dic.Add("山西", "2");
                dic.Add("深圳", "3");
    
                if (!dic.ContainsKey("山西"))
                {
                    dic.Add("山西", "5");
                }
                foreach (KeyValuePair<string, string> item in dic)
                {
                    Console.WriteLine("Key:{0},Value{1}", item.Key, item.Value);
                }
                //排序遍历
                var result = from d in dic orderby d.Key select d;
                foreach (KeyValuePair<string, string> item in result)
                {
                    Console.WriteLine("Key:{0},Value{1}", item.Key, item.Value);
                }
                #endregion
    

     Arrary:

      #region Arrary 创建固定长度的数组
                string[] str = { "1", "2", "3" };
                                int[] i = { 1, 2, 3, 4 };
                                //取数
                                string[] arr = new string[3];
                                arr[0] = "山西";
                                arr[1] = "广州";
                                arr[2] = "上海";
                                Console.WriteLine("{0}",arr.Length);
                #endregion
    

     ArrayList:

      #region  ArrayList 允许动态增加,移除元素,排序、反转数组  using System.Collections;
    
                ArrayList arraylist = new ArrayList();
                arraylist.Add("山西");
                arraylist.Add("北京");
                Console.WriteLine("长度{0}",arraylist.Count);
    
                arraylist.Remove("北京");
                arraylist.RemoveAt(1);
    
                //反转
                arraylist.Reverse();
                //排序
                arraylist.Sort();
                foreach (string s in arraylist)
                {
                    Console.WriteLine(s);
                }
    
                //允许创建元素为类实例的泛型数组
                ArrayList arrList = new ArrayList();
                arrList.Add(new Person(1, "1"));
                foreach (Person item in arrList)
                {
                    Console.WriteLine("item.Id:{0},item.Name:{1}",item.ID,item.Name);
                }
    
                #endregion
    

     List:

      #region  List  创建泛型集合,动态增加、排序、反转 using System.Collections.Generic;
                List<Person> lists = new List<Person>();
                lists.Add(new Person(1, "2"));
                foreach (Person item in lists)
                {
                    Console.WriteLine("item.Id:{0},item.Name:{1}", item.ID, item.Name);
                }
               #endregion
    
  • 相关阅读:
    如何用消息系统避免分布式事务?
    jvm调休,监控
    ClassLoader原理
    JVM结构、GC工作机制详解
    单链表倒置
    hashSet
    HashMap
    hashcode
    深入理解HTTP协议、HTTP协议原理分析
    HTTP协议(详解一)
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/6913541.html
Copyright © 2011-2022 走看看