首页 > 代码库 > 集合、ArrayList 集合。Stack集合。Queue集合。以及Hashtable集合

集合、ArrayList 集合。Stack集合。Queue集合。以及Hashtable集合

arrayList

首先复制Colections加  ; 

创建arrayList ar =new arrayList();

 //ArrayList al=new ArrayList();
            //实例化初始化
            //al.Add(4);
            //真的添加
            //al[0]=3;
            //al[0]这种赋值方式只是修改
            //Console.WriteLine(al[0]);
            //al.Insert(2,"hello");
            //原有的个数加一;
            //al.Clear();

——————————————————————————————————————————————————————————————————————————————

           ArrayList具体提供的功能:
属性                           功能说明
Count        获取 ArrayList 中实际包含的元素数 
方法                           功能说明
Add ()         将对象添加到 ArrayList 的结尾处
Clear()          从 ArrayList 中移除所有元素
Clone()        创建 ArrayList 的浅表副本 
Contains()        确定某元素是否在 ArrayList 中
IndexOf()            已重载。返回 ArrayList 或它的一部分中某个值的第一个匹配项的从零开始的索引 
Insert()        将元素插入 ArrayList 的指定索引处。可在任意位置插入
LastIndexOf()      已重载。返回 ArrayList 或它的一部分中某个值的最后一个匹配项的从零开始的索引
Remove()           从 ArrayList 中移除特定对象的第一个匹配项 
RemoveAt()         移除 ArrayList 的指定索引处的元素 
Sort()             已重载。对 ArrayList 或它的一部分中的元素进行排序,升序
Reverse()                     翻转整个集合

——————————————————————————————————————————————————————————————————————————————

例题输入一些中将手机号码。滚动循环一共三秒钟。加(作弊)

            ArrayList al =new ArrayList();
            Console.Write("请输入人数  ");
            int   a =int.Parse( Console.ReadLine());
            long f=12345678789;


            for (int i = 0; i < a; i++)
            {
                Console.Write("请输入第{0}个人的手机号",(i+1));
                al.Add(Console.ReadLine());   
            }
            Console.Write("所有号码输入完毕");
            Console.ReadLine();
            Random c= new Random();
            for (int i= 0; i< 30; i++)
            {
                int  b = c.Next(a);
                Console.Write("中奖号码是:" +al[b]);
                System.Threading.Thread.Sleep(100);
                Console.Clear();         
            }         
            Console.Clear();
            Console.Write("中奖号码为" + f);
            Console.ReadLine();

————————————————————————————————————————————————————————————————————————————————

例二     

输入班级人数还有每个人的姓名。语文,数学英语成绩。求语文最高分,并且输出姓名分数,数学最低分和姓名,还有英语的平均分

   ArrayList al = new ArrayList();
            Console.Write("请输入班级的人数");
            int a = int.Parse(Console.ReadLine());
            for (int i = 0; i < a; i++)
            {
                Console.Write("请输入第{0}个人的姓名:", i + 1);
                al.Add(Console.ReadLine());
                Console.Write("请输入第{0}个人的语文成绩:", i + 1);
                al.Add(Console.ReadLine());
                Console.Write("请输入第{0}个人的数学成绩:", i + 1);
                al.Add(Console.ReadLine());
                Console.Write("请输入第{0}个人的英语成绩:", i + 1);
                al.Add(Console.ReadLine());
            }
            for (int i = 0; i < 4 * a - 3; i += 4)
            {
                for (int j = i + 4; j < 4 * a - 2; j += 4)
                {
                    if (double.Parse(al[i + 1].ToString()) < double.Parse(al[j + 1].ToString()))
                    {
                        object h = al[i];
                        al[i] = al[j];
                        al[j] = h;
                        object f = al[i + 1];
                        al[i + 1] = al[j + 1];
                        al[j + 1] = f;
                        object g = al[i + 2];
                        al[i + 2] = al[j + 2];
                        al[j + 2] = g;
                        object d = al[i + 3];
                        al[i + 3] = al[j + 3];
                        al[j + 3] = d;
                    }
                }
            }
            Console.Write("{0}的语文成绩最高,是{1}.", al[0], al[1]);
            for (int i = 0; i < 4 * a - 3; i += 4)
            {
                for (int j = i + 4; j < 4 * a - 2; j += 4)
                {
                    if (double.Parse(al[i + 2].ToString()) >double.Parse(al[j + 2].ToString()))
                    {
                        object h = al[i];
                        al[i] = al[j];
                        al[j] = h;
                        object f = al[i + 1];
                        al[i + 1] = al[j + 1];
                        al[j + 1] = f;
                        object g = al[i + 2];
                        al[i + 2] = al[j + 2];
                        al[j + 2] = g;
                        object d = al[i + 3];
                        al[i + 3] = al[j + 3];
                        al[j + 3] = d;
                    }
                }
            }
            Console.Write("{0}的数学成绩最低,是{1}", al[0], al[2]);
            int s = 0;
            for (int i = 0; i <4*a; i+=4)
            {             
                s += int.Parse(al[i+3].ToString());           
            }
            Console.Write(s / a);
            Console.ReadLine();

————————————————————————————————————————————————————————————————————-——————————

  //stack集合
            //    Stack st=new Stack();
            //    st.push(1);
            //    st.push(2);
            //    st.push(3);
            //    st.push(4);
            //    st.push(5);
            ////个数
            //    Console.WriteLine(st.Count);
            ////只要用pop就弹出
            ////只查看。不输出
            ////、Console.WriteLine(st.Peek());
            //    //foreach (int aa in st)
            //    //{
            //    //    Console.WriteLine(aa);
            //    //}

________________________________________________________________________________________________________________________________-

             //队列集合。先进先出
            //    Queue que = new Queue();
            //    que.Enqueue(5);
            //    que.Enqueue(1);
            //    que.Enqueue(3);
            //    que.Enqueue(5);
            //    que.Enqueue(6);
            ////一出一个元素,从头开始
            //    que.Dequeue();
            //    foreach (int aa in que)
            //    {
            //        Console.WriteLine(aa);
            //    }

————————————————————————————————————————————————————————————————————————————————

     //哈希表集合         
           // //一个一个赋值,但是只能一起取值
           // Hashtable ht = new Hashtable();
           //// 添加元素
            //ht.Add(1, "一");
            //ht.Add(2, "二");
            //ht.Add(3, "三");
            //ht.Add(4, "四");
            //ht.Add(5, "五");
            //ht.Add(6, "六");
            //ht.Add(7, "七");
            //foreach (object /*如果不知道就用object*/aa in ht.Keys)//单纯的key的类型
            //{
            //    Console.WriteLine(aa);
            //}
            //foreach (object /*如果不知道就用object*/bb in ht.Values)//单纯的Value的类型
            //{
            //    Console.WriteLine(bb);
            //}
            ////使用枚举类型排列成表格
            //IDictionaryEnumerator ide = ht.GetEnumerator();
            //while (ide.MoveNext())//当ide能够获取到下一个时
            //{
            //    Console.WriteLine(ide.Key + "\t" + ide.Value); //没有s表示一组。有s表示集合
            //}

 

集合、ArrayList 集合。Stack集合。Queue集合。以及Hashtable集合