C#集合与泛型

来源:互联网 发布:罗技优联mac能用吗 编辑:程序博客网 时间:2024/06/05 00:43
集合:


集合:存放一系列元素的集体,元素的个数是可变动的。
理解:长度可变的数组
集合包括泛型集合与非泛型集合
通常我们称泛型集合为泛型为集合
集合里面的类型是不确定的
泛型里面的类型是明确的


ArrayList al = new ArrayList();
al.Add(1);//增加元素
al.Count;集合与泛型中的长度用Count属性获取;
al.Remove();移除指定元素
  int[] arr = { 1,2,3,4};
 al.AddRange(arr);//一次增加多个元素
集合与泛型:Clear(),Add(),Remove()方法或RemoveAt(),Contains()


哈希标,存放的是键值对
不是通过数字索引号获取值,通过键名获取值
键名不能重复,值可以重复
ht.ContainsKey();是否包含键
ht.ContainsValue();是否包含值


ht.Keys;//所有键的集合
ht.Values;//所有的值
HashTable中的每一个元素都是一个键值对
每一个键值对叫一个DictionaryEntry(字典实体)


泛型:


泛型长度可变,类型确定
数组列表对应的泛型
列表
泛型元素确定,就不存在装箱与拆箱操作。比较集合执行效率高
            List<int> list = new List<int>();
与Hashtable对应的有一个字典类型

            Dictionary<string, int> dic = new Dictionary<string, int>();

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;//集合的命名空间


namespace ConsoleApplication1
{
    class Program
    {
        
        static void Main(string[] args)
        {
            #region 集合
            //集合:存放一系列元素的集体,元素的个数是可变动的。
           // 理解:长度可变的数组
            //集合包括泛型集合与非泛型集合
            //通常我们称泛型集合为泛型为集合
            //集合里面的类型是不确定的
            //泛型里面的类型是明确的
            //集合*****************************************


            //ArrayList al = new ArrayList();
            //al.Add(1);//增加元素
            //al.Add("abd");
            //al.Add(true);
            ////al.Count;//集合与泛型中的长度用Count属性获取
            //Console.WriteLine("长度{0}",al.Count);
            //al.Add("321");
            //Console.WriteLine("长度{0}",al.Count);
            //al.Remove(1);//移除
            //Console.WriteLine(al[0]);
            //al[0] = 30;//给某个元素重新赋值,这个元素必须要存在
            //al[2] = 40;
            //al[5] = 50;//错误,不存在
            //al.Clear();//清空 


            //al.Contains();//包含
            //al.RemoveAt();//移除指定索引对应的元素
            //int[] arr = { 1,2,3,4};
          // al.AddRange(arr);//一次增加多个元素
            //集合与泛型:Clear(),Add(),Remove()方法或RemoveAt(),Contains()


            //foreach (object item in al)
            //{
            //    Console.WriteLine(item.ToString());
            //}




            //哈希标,存放的是键值对
            //Hashtable ht = new Hashtable();
            //ht.Add("杨坤",20);
            //ht.Add("- -",21);
            //object obj = ht["- -"];
            //不是通过数字索引号获取值,通过键名获取值
            //键名不能重复,值可以重复
            //ht.ContainsKey();是否包含键
            //ht.ContainsValue();是否包含值


            //ht.Keys;//所有键的集合
            //ht.Values;//所有的值
            //foreach (object item in ht.Keys)
            //{
            //    Console.WriteLine( "key{0}value{1}",item,ht[item]);
            //}
            //HashTable中的每一个元素都是一个键值对
            ////每一个键值对叫一个DictionaryEntry(字典实体)
            //foreach (DictionaryEntry item in ht)
            //{
            //    Console.WriteLine("key{0}value{1}",item.Key,item.Value);
            //}




            //泛型长度可变,类型确定
            //数组列表对应的泛型
            //列表
            //泛型元素确定,就不存在装箱与拆箱操作。比较集合执行效率高
            //List<int> list = new List<int>();
            //list.Add(10);
            //list.Add(20);
            //list.Add(30);
            //list.Add(40);
            //list.Add(50);
            //Console.WriteLine(list[0]);
            //Console.WriteLine("长度{0}",list.Count);
            //foreach (int item in list)
            //{
            //    Console.WriteLine(item);
            //}
            //for (int i = 0; i < list.Count; i++)
            //{
            //    Console.WriteLine(list[i]);
            //}


            //List<Student> list = new List<Student>();
            //Student stu;
            //stu.Name = "坑爹";
            //stu.Sex="nv";
            //stu.Age = 21;
            //list.Add(stu);
            //Student stu2;
            //stu2.Name = "杨坤";
            //stu2.Sex = "男";
            //stu2.Age = 20;
            //list.Add(stu2);
            //foreach (Student item in list)
            //{
            //    Console.WriteLine("{0}\t{1}\t{2}",item.Name,item.Sex,item.Age);
            //}


            //List<List<Student>> list = new List<List<Student>>();


            //与Hashtable对应的有一个字典类型
            //Dictionary<string, int> dic = new Dictionary<string, int>();
            //dic.Add("杨坤", 20);
            //dic.Add("- -", 21);
            //dic.Add("王婷婷",20);
            //dic.Remove("- -");//移除


            ////dic.Keys;
            ////dic.Values;
            //foreach (string item in dic.Keys)
            //{
            //    Console.WriteLine("{0}\t{1}",item,dic[item]);
            //}
            ////遍历每一个简直对
            //foreach (KeyValuePair<string,int> item in dic)
            //{
            //    Console.WriteLine("{0}\t{1}",item.Key,item.Value);
            //}
#endregion


            #region 数学
            //ArrayList list=new ArrayList();
            //list.Add();
            //方法使用:有些方法用的是类的对象点出来的,有的用类名点出来的
            //int b= Math.Abs(-10);//绝对值
            //Math中提供了很多的数学计算的方法
            //Console.WriteLine(Math.BigMul(2,3));//两个整数的乘积
            //Console.WriteLine(Math.Ceiling(10.5));//整数部分加一
            //Console.WriteLine(Math.Floor(10.5));//整数部分
            //Console.WriteLine(Math.Exp(1));//求n次幂e=2.718
            //Console.WriteLine(Math.Log10(100));//求以10为底的对数
            //Console.WriteLine(Math.Max(20,30));//比较取较大的数
            //Console.WriteLine(Math.Min(20,30));//比较取较小的数
            //Console.WriteLine(Math.Pow(2,3));//求两个数的幂
            //Console.WriteLine(Math.Round(4.5));//整数部分为奇数整个数五入,整数部分为偶数五是舍
            //Console.WriteLine(Math.Round(3.5));
            //Console.WriteLine(Math.Round(4.4));
            //Console.WriteLine(Math.Sqrt(25));//求平方根
            //Console.WriteLine(Math.Truncate(12.5));//整数部分
            //Console.WriteLine(Math.PI);
            //练习:
            //一元二次方程求解
            //Console.WriteLine("输入一元二次方程的a,b,c;用逗号隔开:");
            //string str = Console.ReadLine();
            //double a, b, c;
            //string[] arr = str.Split(',');
            //a = double.Parse(arr[0]);
            //b = double.Parse(arr[1]);
            //c = double.Parse(arr[2]);
            ////1判断有没有解
            //double d=Math.Pow(b,2)-4*a*c;
            //if (d>0)
            //{
            //    //两个解
            //    double  x1=((-1)*b+Math.Sqrt(d))/(2*a);
            //    double x2 = ((-1) * b - Math.Sqrt(d)) / (2 * a);
            //    Console.WriteLine("两个解分别为{0}\t{1}",x1,x2);
            //}
            //else if (d == 0)
            //{
            //    double x = ((-1) * b) / (2 * a);
            //    Console.WriteLine("解为{0}",x);
            //    //一个jie
            //}
            //else
            //{
            //    Console.WriteLine("无解");
            ////无解
            //}




             #endregion


            #region 时间


            //DateTime dt = DateTime.Now;//当前时间
            //DateTime dt2 = new DateTime(2012, 12, 21);//定义一个时间
            //long tick = dt.Ticks;//时间刻度,标识一个时间的数字
            //dt.Year;//年份
            //dt.Month;//月份
            //dt.Day;//日
            //dt.Millisecond//毫秒
           //DayOfWeek week= dt.DayOfWeek;//星期
           //Console.WriteLine(week);
          
            //Console.WriteLine(  dt.DayOfYear);
            //DateTime dta = dt.Date;
            //Console.WriteLine("{0}\n{1}",dt,dta);
            ////Add方法
            //DateTime dt3 = dt.AddDays(3);
            //Console.WriteLine(dt3);
            ////ToString()方法 
            //Console.WriteLine("短时间{0}",dt.ToShortTimeString());
            //Console.WriteLine("长时间格式{0}",dt.ToLongTimeString());


            //TimeSpan ts = dt - dt2;//TimeSpan存放时间的时隔
            //ts中的属性
            //Console.WriteLine( ts.Days);//只算天数
            //Console.WriteLine(ts.TotalDays);//总天数,以小数返回,精确


            //万年历**************************
            //1.输入一个时间
            //Console.WriteLine("请输入一个时间:");
            //string Date = Console.ReadLine();
            // DateTime dt= DateTime.Parse(Date);
            //int year=dt.Year;
            //int month = dt.Month;
            //int day = dt.Day;
            ////2.这个时间对应的月份的第一天星期几
            //DateTime dtFirst = new DateTime(year, month, 1);
            //int week=(int)dtFirst.DayOfWeek;
            ////3.这个月一共有多少天
            ////下个月一号与这个月一号的差
            //DateTime dtNextMonth = dtFirst.AddMonths(1);
            //TimeSpan ts = dtNextMonth - dtFirst;
            //int days = ts.Days;
            ////4.打印
            //Console.WriteLine("日\t一\t二\t三\t四\t五\t六");
            //for (int i = 0; i < week; i++)
            //{
            //    Console.Write("\t");
            //}


            //for (int i = 1; i <= days; i++)
            //{


            //    if (i == day)
            //    {
            //        Console.ForegroundColor = ConsoleColor.Red;
            //        Console.Write("{0}\t", i);
            //        Console.ForegroundColor = ConsoleColor.White;


            //    }
            //    else
            //    { 
            //    Console.Write("{0}\t", i);
            //    }
                
            //    if ((week+i)%7==0)
            //    {
            //        Console.WriteLine();
            //    }
            //}
          
            
            
            #endregion


            #region 随机


            //Random s = new Random();//随机对象
            ////产生随机数
            //double db= s.NextDouble();
            ////产生0~1之间的小数,包括下限,不包括上限,可以产生0,不可以产生1
            //Console.WriteLine(db);


            //byte[] buffer=new byte[10];
            //s.NextBytes(buffer);
            //foreach (byte item in buffer)
            //{
            //    Console.WriteLine(item);
            //}
            //int i = s.Next();
            //i = s.Next(10);//0~10的数,不包括10
            //i = s.Next(1,100);
            //Console.WriteLine(i);


            //双色球
            //红球,蓝球
            //1~33红色选6个
            //1~16蓝色选1个


           // while (true)
           // {
           //     Console.SetCursorPosition(0,0);//设置位置每次,在指定位置打印
           //     //Console.Clear();//清屏
           //         int[] arr = new int[6];
           // int blue=0;
           // Random r = new Random();
           // blue = r.Next(1, 17);//蓝球
           // for (int i = 0; i < 6; i++)
           // {
           //     arr[i] = r.Next(1, 34);
           // }
           // Console.ForegroundColor = ConsoleColor.Red;
           // foreach (int item in arr)
           // {
           //     Console.Write("{0,2}\t",item);
           // }
           //Console.ForegroundColor = ConsoleColor.Blue;
           // Console.Write("{0,2}",blue);
             
           // if (Console.KeyAvailable)
           // {
           //    ConsoleKeyInfo key= Console.ReadKey(true);//得到一个按键的信息;true不在控制台下出现按下的键
           //     //这个按下的键
           //    ConsoleKey ck = key.Key;
           //    if (ck==ConsoleKey.Enter)
           //    {
           //      break;  
           //    }
               
           // }
                
           // }




            
           


            #endregion
        }
    }


    struct Student
    {
        public string Name;
        public string Sex;
        public int Age;
    }
}

原创粉丝点击