zoukankan      html  css  js  c++  java
  • 2017-3-2 C#基础 集合

    要使用集合必须先引用命名空间,using System.Collections;

    集合与数组的不同

    数组:同一类型,固定长度
    集合:不同类型,不固定长度

    集合主要分为六大类:普通集合,泛型集合,哈希表集合,字典集合,队列集合,栈桥集合。

    定义
    ArrayList arr = new ArrayList();(普通集合,弱类型集合

    赋值
    arr.Add("值/变量"); //object类型
    object类型:所有类型的基础类型(基类)

    获取个数
    arr.Count;

    取值
    arr[索引]

    插队
    arr.Insert(索引,值/变量)

    移除
    arr.Remove(值);
    arr.RemoveAt(索引);

    反转
    arr.Reverse(); - 全部反转
    arr.Reverse(索引,个数); - 指定反转

    清空
    arr.Clear();

    List<T> T:强类型集合
    List<int> slist = new List<int>();

    哈希表集合:弱类型
    Hashtable hs = new Hashtable();

    字典:强类型
    Dictionary<int, string> dic = new Dictionary<int, string>();

    队列集合

    Queue q=new Queue();

    添内容:q.Enqueue();

    取内容:q.Dequeue(); 从第一个开始往外输出

    栈桥集合:

    Stack st=new Stack();

    添内容:st.push();

    取内容:st.pop();  从最后一个开始往外输出

    代表习题:1、集合筛选 1-100 筛选成 50-90 

    namespace _2017_3_2___集合
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<int> a = new List<int>();
                int i = 0;
                
                    for ( i = 1; i < 101; i++)
                    {
                        a.Add(i);
    
                    }
                    //Console.Write(a.Count);
                 foreach(int b in a)
                 {
                     if (b >= 50 && b <= 90)
                     {
                         Console.WriteLine(b);
                     }
                 }
                 Console.ReadLine();
                }           
                     
        }
    }

     集合筛选 1-100 筛选成 50-90    让集合里只剩下50-90的数

    namespace _2017_3_2__集合__打印50_90__2
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                List<int> a = new List<int>();
                int i = 0;
    
                for (i = 1; i < 101; i++)
                {
                    a.Add(i);
    
                }
                //Console.Write(a.Count);
                List<int> a1 = new List<int>();
                foreach (int b in a)
                {
                    if (b>=50 &&b <=90)
                    {
                        a1.Add(b);
                    }
                }
                a = a1;
                a1 = null;
                Console.WriteLine(a.Count);
               
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    alt属性和title属性
    穷人和富人的区别
    JS经典源码:通用JavaScript脚本函数库
    网页弹出框
    男女交往必知15个真理
    alexa排名
    好男人找不到女朋友的根源
    小笑话集(全是经典!)不信你不笑(转)
    送到家服务网2009年3月26日顺利上线了!
    [转]互联网产品开发中的“快”字诀
  • 原文地址:https://www.cnblogs.com/zhengqian/p/6489589.html
Copyright © 2011-2022 走看看