2017-3-2 集合 结构体 枚举

(一)集合

1.定义:不同类型,不固定长度。

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

  ArrayList  变量名 = new ArrayList(); ---------弱类型集合

2.赋值:  ArrayList aa = new ArrayList();  

      aa.Add(值/变量);  //返回object类型,object类型是所有类型的基础

3.获取集合的长度   aa.count ;

4.取值  aa[索引] 

5.插队 aa.Insert(索引,值/变量)aa.Insert(2,50);  2是字符串的位置索引  50 是要插入的数字

6.移除 arr.Remove(值);    aa.Remove(40)----移除40

    aa.RemoveAt(索引);    移除的是对应索引的值

8. 集合字符串反转   aa.Reverse();    aa.Reverse(1,2);-----1表示下标,2表示反转的长度

9.aa.clear     清空

强类型集合    声明数据类型

List<T> T:泛型,任何类型   ,自己创建的student类,也可以List<student> list = new List<student>();
List<int> slist = new List<int>();

弱类型集合   

哈希表集合  Hashtable ha = new Hashtable();

 static void Main(string[] args)
        {

            Hashtable ha = new Hashtable();
            //      (键,值)  键值对   键  值 都是object类型,积类型
            ha.Add(1,"哈哈");     
            ha.Add("aaaa","嘻嘻嘻嘻"); 
            
            //取值   取值的时候注意,要分开键keys,值values,
            foreach(string s in ha.Values)
            {
                Console.WriteLine(s);
            }
             Console.ReadKey();
        }


字典:强类型  声明键值对的数据类型
Dictionary<int, string> dic = new Dictionary<int, string>();

特殊集合

队列集合,输出的时候先进先出  Queue  qe = new Queue();  添加数据qe.Enqueue(object类型);  打印 qe.Dequeue

栈桥集合 ,输出的时候先进后出  Stack  aa = new Stack();     添加数据 aa.Push(object类型));   打印 qe.Pop

(二) contains方法

判断字符串中是否有某个字符

static void Main(string[] args)
        {
            List<string> aa = new List<string>();
            aa.Add("aaaa");
            aa.Add("bbbb");
            aa.Add("cccc");
            aa.Add("dddd");
            //判断字符串中是否含有bbbb
            bool has = false;
            has = aa.Contains("bbbb");
            Console.WriteLine(has);

            //判断字符串中是否含有bb
            bool has1 = false;
            foreach(string a in aa)
            {
                if(a.Contains("b"))
                {
                     has1 = true;
                }
            }
            Console.WriteLine(has1);
            Console.ReadLine();
            
        }

(三)结构体

  用户自定义函数,位置定义在Main函数的外面,类的里面

定义格式:

struck  自定义名称

{

  public 数据类型 名字;
  public 数据类型 名字;
  ...
  ...

}

声明实例化:
结构体类型 ss = new 结构体类型();
Student s = new Student();

(四)枚举

统一记录数据格式

 class Program
    {
        enum Sex
        {
            男,女
        }
        enum Week 
        {
            星期一,
            星期二,
            星期三,
            星期四,
            星期五,
            星期六,
            星期日
        
        }
        static void Main(string[] args)
        {
            Sex s = Sex.男;
            Sex sd = Sex.女;

            Week w = Week.星期二;

            Console.ReadLine();
        }

 

 

posted @ 2017-03-02 21:24  青年a  阅读(149)  评论(0编辑  收藏  举报