zoukankan      html  css  js  c++  java
  • C#基础 集合、特殊集合(哈希表集合、 字典、队列集合、栈桥集合)

    一集合

    1、集合特点

    (1)、可为不同类型,不固定长度

    (2)、集合类型分为:泛型集合(强类型集合)与非泛型集合(弱类型集合)。

    (3)、非泛型集合的类和接口位于using System.Collections命名空间,泛型集合的类和接口位于using System.Collections.Generic命名空间。

    2、表达式

    List <int>  arr = new List<int>();       //强类型  ,每个集合中只含有同一种类型
    
    ArrayList  arr  =  new ArrayList();       //弱类型 

    3、集合赋值

    ArrayList  arr  =  new ArrayList(); 
    arr.Add(  10  );C 
    arr.Add( "aaa" );   //  赋值    集合名 . Add( );  
    arr.Add( 10.5 );               
    arr.Add( true );                               
    arr.Add(  dt  );

    4、集合取值

    //取指定值,( 集合名[索引] )
     arr [ 索引 ] ;        
    
    // foreach  遍历,取所有值
    foreach ( int i  in arr )                       
    {
        Console.WriteLine( i );             
    }

    5、常用方法函数

       

    // 插队
     arr.Insent ( 索引 , 值/变量 );  
            //  arr.Insent( 2,30 );   在索引 2 的位置上插入值为 30 的数
    //移除 
     arr .Remove( 值  )                  
     arr.RemoveAt( 索引 )            
            //arr.Remove( 10 )     移除第一个与值相匹配的对象。
            //arr.RemoveAt( 1 )   移除索引 1 所标记的对象。
     //反转  
    arr.Reverse(  );       
    arr.Reverse(  索引 , 值  );      
          // arr.Reverse( );       全部反转
          // arr.Reverse( 1,3 );  从索引 1 (包含)开始往后 3 个对象进行反转
    
    //清空   
      arr.Clear();        
    //计算个数
      arr.Count;     //集合是一个开放性的没有长度只有个数
    //是否包含
      arr.Contains(  );     // 对所有集合都适用                         


    6、实例

    List <string> sl = new  List<string>();
    sl.Add("aaa");
    sl.Add("bbb");
    sl.Add("ccc");
    
    //1、是否包含"bbb"
    //(1)
    bool has = false;
    foreach (string s in sl)
    {
        if ( s=="bbb")
        has = true
    }
    
    //(2)
    bool  has = sl.Contains("bbb");
    Console.WriteLine( has );
    
    //2、是否包含"b"
    bool has = false
    foreach (string s in sl )
    {
        if ( s .Contins(" b ") )
        has = ture
    }

     二 特殊集合

    (一) 哈希表集合(弱类型)

             弱类型,用户自定义索引。不能插队,不能反转,可删可清。

    表达式

    Hashtable  hs  =  new Hashtable ();
    
    //赋值,键值对赋值, 键:keys  值:values
    hs . Add( 建,值);     
    //取值     
    hs [建]        

    (二) 字典(强类型)

     "键" 同一类型。"值" 同一类型。在使用前,你必须声明它的键类型和值类型。

     表达式

      Dictionary  <键 , 值>  dic  =  new  Dictionary  <键 , 值>  (  );
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Demo1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //创建泛型哈希表,Key类型为int,Value类型为string
                Dictionary<int, string> myDictionary = new Dictionary<int, string>();
                //1.添加元素
                myDictionary.Add(1, "a");
                myDictionary.Add(2, "b");
                myDictionary.Add(3, "c");
                //2.删除元素
                myDictionary.Remove(3);
                //3.假如不存在元素则添加元素
                if (!myDictionary.ContainsKey(4))
                {
                    myDictionary.Add(4, "d");
                }
                //4.显示容量和元素个数
                Console.WriteLine("元素个数:{0}",myDictionary.Count);
                //5.通过key查找元素
                if (myDictionary.ContainsKey(1))
                {
                    Console.WriteLine("key:{0},value:{1}","1", myDictionary[1]);
                    Console.WriteLine(myDictionary[1]);            
                }
                //6.通过KeyValuePair遍历元素
                foreach (KeyValuePair<int,string>kvp in myDictionary)
                {
                    Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value);
    
                }
                //7.得到哈希表键的集合
                Dictionary<int, string>.KeyCollection keyCol = myDictionary.Keys;
                    //遍历键的集合
                    foreach (int n in keyCol)
                    {
                        Console.WriteLine("key={0}", n);                
                    }
                //8.得到哈希表值的集合
                Dictionary<int, string>.ValueCollection valCol = myDictionary.Values;
                    //遍历值的集合
                    foreach( string s in valCol)
                    {
                    Console.WriteLine("value:{0}",s);
                    }
                //9.使用TryGetValue方法获取指定键对应的值
                string slove = string.Empty;
                if (myDictionary.TryGetValue(5, out slove))
                {
                    Console.WriteLine("查找结果:{0}", slove);
                }
                else
                {
                    Console.WriteLine("查找失败");
                }
                //10.清空哈希表
                //myDictionary.Clear();
                Console.ReadKey();
            }
        }
    }

    (三) 队列集合                  

         先进先出

    表达式

    Queue  q  = new  Queue( );
    
    //赋值  
    q . Enqueue( );    
    
    //取值  
    q . Dequeue( );       // 返回并移除集合中的对象。

    (四) 栈桥集合                    

      先进后出

    表达式

    Stack  st  =  new  Stack;
    
    //赋值  
    st . push ( );
    
    //取值  
    st . pop ( );              //返回并移除
  • 相关阅读:
    如何在博客园中添加新随笔以及随笔参考格式
    如何在班级博客中提交作业
    C高级第一次作业
    MySQL调优
    个人作业--软件工程实践总结
    动态分区最佳-最坏-最先分配
    动态分区代码
    个人作业-软件评测
    软件工程第五次作业
    软件工程第四次作业——结对作业
  • 原文地址:https://www.cnblogs.com/Tanghongchang/p/6498803.html
Copyright © 2011-2022 走看看