c# 6大类集合

数组跟集合的区别

数组:长度固定,类型相同;

集合:不固定类型,不固定长度;

 

要使用集合,必须先引用命名空间:

在最上方using System.Collections;

 

定义创建一个新的集合

ArrayList 集合名称 =new ArrayList();

赋值:集合名称.Add(object类型);  object类型(基础类型):stirng,double,bool,int任意类型的都可以。    

取值:集合名称[索引];          集合索引按先后输入顺序排列

集合名称.Count;集合的长度

变量名.Insert(要插入位置的索引,要插入的值);

数组名称.Remove(值)移除数组的第一个值

集合名称,RemoveAt(索引);    移除对应索引的值。

集合名称.();               将原本顺序输出改为倒序输出。 

集合名称.(要开始倒序输出的索引,要倒序输出的个数)

集合名称.Clear();清空这个集合

 

泛型集合

泛型集合针对同一类型的集合 属于强类型集合

定义: List<指定基类(int/string)> 集合名称 =new List <与前面指定基类相同>();跟A

rrayList的代码一样

 

 

哈希表集合     弱类型、

可以自定义索引的集合

定义:Hashtable 集合名称 =new Hashtable();

赋值:变量名称.Add(自定义的索引(字符串索引加""),值);    键值对,一个键对应一个值。

不可以插队 也不可以反转

 

字典集合     强类型

定义 Dictionary<索引类型,值类型> 集合名称 =Dictionary<与前面一样索引类型,与前面一样值类型>();

 

队列集合    

定义:Queue 集合名称=  new Queue();

队列集合 输出是先进去的先出来

赋值:集合名称.Enqueue(值);

取值:集合名称.Dequeue();

 

栈桥集合

定义:Stack 集合名称=new Stack();

与队列集合相反 先进去的后出来

赋值: 集合名称.Push(值);

取值:集合名称.Pop(); 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _3_2作业结构体
{
    class Program
    {
        struct student
        {
            public string xuehao;
            public string name;
            public DateTime dt;
            public int cj;


        }

        static void Main(string[] args)
        {
            Console.Write("请输入录入学生的个数");
            int a = Convert.ToInt32( Console.ReadLine());

            List< student >list= new List<student>();
           
            for (int i = 1; i <= a; i++)
            {
                student s = new student();
                Console.Write("请输入第"+i+"个学生的学号:");
                s.xuehao = Console.ReadLine();
                Console.Write("请输入第" + i + "个学生的姓名:");
                s.name = Console.ReadLine();
                Console.Write("请输入第" + i + "个学生的生日:");
                s.dt = Convert.ToDateTime(Console.ReadLine());
                Console.Write("请输入第" + i + "个学生的成绩:");
                s.cj = Convert.ToInt32(Console.ReadLine());

                list.Add(s);
            }
            Console.WriteLine("===================学生信息展示===========================");
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = i+1; j < list.Count;j++ )
                    if (list[i].cj < list[j].cj)
                    {
                        student sss = list[i];
                        list[i] = list[j];
                        list[j] = sss;
                    }
            }
            foreach (student s in list)
            {
                int age = DateTime.Now.Year - s.dt.Year;
                string sr = s.dt.ToString("yyyy年MM月dd日");
                Console.WriteLine(s.xuehao + "\t" + s.name + "\t" +sr + "\t" + age + "\t" + s.cj);
            }

                    Console.ReadLine();
        }

 

 

 

      

posted @ 2017-03-04 21:00  v587yy  阅读(350)  评论(0编辑  收藏  举报