zoukankan      html  css  js  c++  java
  • 集合


       public class Student
        {
            public string StuNo { get; set; }
            public string StuName { get; set; }
            public int StuAge { get; set; }


            }

    class Program
        {
            /*****************ArrayList使用(动态数组)*********************/
            //特点:有索引,每个元素对应一个索引,可以存储任何类型,以object类型存入,取出来也是object类型
            //static void Main(string[] args)
            //{
            //    ArrayList arrayList = new ArrayList();
            //    Console.WriteLine("集合容量:" + arrayList.Capacity);
            //    arrayList.Add(5);//在末尾添加元素
            //    arrayList.Add("abc");
            //    arrayList.Add(true);
            //    Console.WriteLine("集合容量:" + arrayList.Capacity);
            //    Console.WriteLine("集合中元素的个数:" + arrayList.Count);
            //    arrayList.Add(21.3);
            //    arrayList.Add(null);
            //    //使用无参构造方法创建的集合对象初始容量为0,添加一个元素,容量扩大到4,如果添加的元素个数超过4个,容量会翻倍
            //    Console.WriteLine("集合容量:" + arrayList.Capacity);
            //    Print(arrayList);
            //    arrayList.Remove("abc");//删除指定元素
            //    arrayList.RemoveAt(3);//删除指定索引位置的元素
            //    Console.WriteLine("删除后:");
            //    Print(arrayList);
            //    int[] numbers = { 20,30,40};
            //    arrayList.AddRange(numbers);//添加一系列元素
            //    Print(arrayList);
            //    arrayList.Insert(2,100);//插入元素到指定位置
            //    Print(arrayList);
            //    arrayList.Reverse();//倒置
            //    Print(arrayList);
            //    Console.WriteLine(arrayList.Contains(100));
            //}

            //public static void Print(ArrayList arr)
            //{
            //    Console.WriteLine("集合中的元素如下:");
            //    //遍历方法一
            //    //for (int i = 0; i < arr.Count; i++)
            //    //{
            //    //    Console.WriteLine(arr[i]);
            //    //}
            //    //遍历方法二
            //    foreach (var item in arr)
            //    {
            //        Console.WriteLine(item);
            //    }
            //}


            /*****************HashTable使用(哈希表)*********************/
            //特点:没有索引,是以键-值(key-value)对的形式存储数据,以object类型存入,取出来也是object类型
            //static void Main(string[] args)
            //{
            //    Hashtable hashTable = new Hashtable();
            //    hashTable.Add("张三", "13629182938");
            //    hashTable.Add("李四", "13456258456");
            //    hashTable.Add("王五", "13522232547");
            //    hashTable.Add("赵六", "13655689452");
            //    //hashTable.Add("赵六", "13655689666");//出异常,因为key中有2个“赵六”
            //    Console.WriteLine(hashTable["李四"]);//根据键获取值
            //    Console.WriteLine("遍历键:");
            //    foreach (var item in hashTable.Keys)
            //    {
            //        Console.WriteLine(item);
            //    }
            //    Console.WriteLine("遍历值:");
            //    foreach (var item in hashTable.Values)
            //    {
            //        Console.WriteLine(item);
            //    }
            //    Console.WriteLine("根据键遍历值");
            //    foreach (var item in hashTable.Keys)
            //    {
            //        Console.WriteLine(item + "-" + hashTable[item]);
            //    }
            //    Console.WriteLine(hashTable.ContainsKey("李四1"));//判断键集合中是否包含某个元素
            //    Console.WriteLine(hashTable.ContainsValue("13456258456"));//判断值集合中是否包含某个元素
            //    hashTable.Remove("张三");
            //    Console.WriteLine(hashTable.Count);
            //}


            /*****************使用集合存储对象**********************/
            //static void Main(string[] args)
            //{
            //    //ArrayList存储对象
            //    //Student stu1 = new Student(){StuNo="S001",StuName="张三",StuAge=23};
            //    //Student stu2 = new Student(){StuNo="S002",StuName="李四",StuAge=24};
            //    //Student stu3 = new Student(){StuNo="S003",StuName="王五",StuAge=25};
            //    //ArrayList arrayList = new ArrayList();
            //    //arrayList.Add(stu1);
            //    //arrayList.Add(stu2);
            //    //arrayList.Add(stu3);
            //    //foreach (var item in arrayList)
            //    //{
            //    //    Console.WriteLine(((Student)item).StuName);
            //    //}

            //    //HashTable存储对象
            //    Student stu1 = new Student() { StuNo = "S001", StuName = "张三", StuAge = 23 };
            //    Student stu2 = new Student() { StuNo = "S002", StuName = "李四", StuAge = 24 };
            //    Student stu3 = new Student() { StuNo = "S003", StuName = "王五", StuAge = 25 };
            //    Hashtable hashTable = new Hashtable();
            //    hashTable.Add(stu1.StuName, stu1);
            //    hashTable.Add(stu2.StuName, stu2);
            //    hashTable.Add(stu3.StuName, stu3);
            //    Student stu = hashTable["李四"] as Student;
            //    Console.WriteLine(stu.StuNo+"-"+stu.StuName+"-"+stu.StuAge);
            //}

            /******普通集合的缺点(1、需要类型判断 2、需要类型转换)***********/
            //static void Main(string[] args)
            //{
            //    ArrayList arr = new ArrayList();
            //    Student stu1 = new Student() { StuName = "张三", StuNo = "S001", StuAge = 23 };
            //    Student stu2 = new Student() { StuName = "李四", StuNo = "S002", StuAge = 24 };
            //    Teacher teacher = new Teacher() { TeacherName = "小明" };
            //    arr.Add(stu1);
            //    arr.Add(stu2);
            //    arr.Add(teacher);
            //    foreach (object item in arr)
            //    {
            //        if(item is Student)
            //        {
            //            Console.WriteLine(((Student)item).StuName);
            //        }
            //        else if(item is Teacher)
            //        {
            //            Console.WriteLine(((Teacher)item).TeacherName);
            //        }
            //    }
            //}


            /***********List<T>泛型集合的使用****************/
            //static void Main(string[] args)
            //{
            //    List<int> list = new List<int>();
            //    list.Add(10);
            //    //list.Add(22.3);//出错,因为22.3不是int类型
            //    list.Add(30);
            //    List<Student> students = new List<Student>();
            //    Student stu1 = new Student() { StuName = "张三", StuNo = "S001", StuAge = 23 };
            //    Student stu2 = new Student() { StuName = "李四", StuNo = "S002", StuAge = 24 };
            //    students.Add(stu1);
            //    students.Add(stu2);
            //    Console.WriteLine(students[0].StuName);
            //    Teacher teacher = new Teacher() {TeacherName="小明" };
            //    //students.Add(teacher);//出错,类型不匹配
            //}

            /***********Dictionary<K,V>泛型集合的使用****************/
            //static void Main(string[] args)
            //{
            //    Dictionary<string, Student> students = new Dictionary<string, Student>();
            //    Student stu1 = new Student() { StuName = "张三", StuNo = "S001", StuAge = 23 };
            //    Student stu2 = new Student() { StuName = "李四", StuNo = "S002", StuAge = 24 };
            //    students.Add(stu1.StuNo, stu1);
            //    students.Add(stu2.StuNo, stu2);
            //    foreach (Student stu in students.Values)
            //    {
            //        Console.WriteLine(stu.StuName);
            //    }
            //}

  • 相关阅读:
    [luogu]P1852跳跳棋
    StdDraw绘图
    Java-Timer-Stop
    人之初
    单例模式--延时初始化
    ubuntu忘记密码
    QT5 TK1 串口通信
    金秋十月
    级联分类器训练-----OpenCV
    Hu矩SVM训练及检测-----OpenCV
  • 原文地址:https://www.cnblogs.com/danmao/p/3871802.html
Copyright © 2011-2022 走看看