【2017-03-02】集合、结构体、枚举

集合和数组的差别:

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

一、普通集合(弱类型)

1、ArryList 使用集合首先要引用命名空间。

或者在ArryList上右键找“解析”。

2、集合的定义: ArryList  arr =new ArryList();

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

4、获取个数:

arr.Count;

5、取值:
arr[索引]     和数组一样。

6、打印:

1)for(int i=0;i<arr.Count;i++)

{

        Console.WriteLine(arr[i]);   //其中i是索引值,可以打印任何类型。

}

2) foreach(int i in arr)

{

       Console.WriteLine(i);        //其中i是int类型,foreach用于都是同一类型的打印。

}

7、插队:
arr.Insert(索引,值/变量);         //将索引位置换位插入的值/变量,将索引位置原来的值/变量往后推一位。

8、移除:
arr.Remove(值);                  //移除的是一个值,从0开始只移除第一个匹配项。
arr.RemoveAt(索引);            //移除的是索引位置对应的值

9、反转:
arr.Reverse(); - 全部反转
arr.Reverse(索引,个数); - 指定反转         //从第几个开始反转,反转几个

10、清空:
arr.Clear();

二、泛型集合List(强类型)   固定类型,不固定长度

命名空间:using System.Collections.Generic;

List<T>                           // T:泛型,T可以为任何类型。
List<int> slist = new List<int>();

所有的操作同普通集合,都一样!!!!!!!!

三、哈希表集合

注意命名空间

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

1、赋值

       // (键, 值)       键是object类型,即所有类型都可以。

hs.Add(1,"呵呵");

hs.Add("aaa","哈哈");

2、取值

Console.Write(hs[1]);

foreach(string s in hs.values)

{

      Console.write(s);

}

四、字典

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

 dic.Add(键,值)           // 赋值里的键、值必须与定义的键、值是同类型


五、特殊集合(队列集合、栈桥集合)

队列集合:先进去的先出来

Queue  q =new queue();             //Queue 右键解析一下

1、赋值

q.Enqueue("aaa");

q.Enqueue(111);

2、取值

Console.write(q.Dequeue());       //括号内没有参数     输出"aaa"

栈桥集合:先进去的后出来,后进去的先出来。

Stack st =new Stack();

1、赋值

st.Push("aaa");

st.Push(111);

2、取值

Console.write(st.Pop());            //括号内没有参数    输出111

六、结构体

结构体:用户自定义类型
定义位置:定义在Main函数的外面,类的里面

1、定义格式:
struct 自定义名字
{
public 数据类型 名字;
public 数据类型 名字;
...
...
}

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

赋值:
s.???=???
s.???=??? 

取值:
s.???

 

作业题

结构体练习题:
“请输入录入学生的个数:”

记录学生信息,需要输入
“请输入第1个学生的学号”
“请输入第1个学生的姓名”
“请输入第1个学生的生日”
“请输入第1个学生的成绩”

打印格式如下:
================学生信息展示===================
s001 张三 2000年1月1日 17 90
s002 李四 2000年1月1日 17 80

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

namespace 练习题讲解
{
    class Program
    {
        struct Student
        {
            public string code;
            public string name;
            public DateTime birthday;
            public double score;
        }


        static void Main(string[] args)
        {
            #region 录入学生信息
            List<Student> list = new List<Student>();   //定义一个泛型集合

            Console.Write("请输入学生个数:");
            int count = Convert.ToInt32(Console.ReadLine());

            for (int i = 1; i <= count; i++)
            {
                Student s = new Student();
                Console.Write("请输入第" + i + "个学生的学号:");
                s.code = Console.ReadLine();
                Console.Write("请输入第" + i + "个学生的姓名:");
                s.name = Console.ReadLine();
                Console.Write("请输入第" + i + "个学生的生日:");
                s.birthday = Convert.ToDateTime(Console.ReadLine());
                Console.Write("请输入第" + i + "个学生的成绩:");
                s.score = Convert.ToDouble(Console.ReadLine());
                list.Add(s);     //将填完的学生信息保存到准备好的集合里
            }
            #endregion

            #region 冒泡排序计算
            for (int i = 0; i < list.Count - 1; i++)
            {
                for (int j = i + 1; j < list.Count; j++)
                {
                    if (list[i].score < list[j].score)
                    {
                        Student sss = list[i];                 //如果i成绩小于j成绩,则将两者全部调换位置,不是只调换分数
                        list[i] = list[j];
                        list[j] = sss;
                    }
                }
            }
            #endregion

            Console.WriteLine("================学生信息展示===================");
            foreach (Student s in list)
            {
                int age = DateTime.Now.Year - s.birthday.Year;//用现在时间的年份减去生日的年份等于年龄。

                Console.WriteLine(s.code + "	" + s.name + "	" + s.birthday.ToString("yyyy年MM月dd日") + "	" + age + "	" + s.score);
            }

            Console.ReadLine();
        }
    }
}

 七、枚举

用户自定义类型     在Main函数外边,在类里边。
用来统一数据格式的

enum Week
{
星期一,
星期二,
星期三,
星期四,
星期五,
星期六,
星期日
}

原文地址:https://www.cnblogs.com/qq609113043/p/6489953.html