zoukankan      html  css  js  c++  java
  • 2016.7.28C#基础 集合

        ArrayList


                ArrayList myarry = new ArrayList();
                myarry.Add(1);//添加元素
                myarry.Add(2);//索引也是从零开始
                myarry.Add(3);
                myarry.Add(4);
                myarry.Add(5)
    ;

               myarry.Insert(3,"7");          

               myarry.Insert(3,"7");
               插入在索引号为3的位置一个为7的值
               后面的索引依次向后+1     

               myarry.Remove(4);
               移除数据为4的元素
               括号里面是想要移除的数据

               
               myarry.RemoveAt(4);
               移除索引号为4的数据
                
               int aa = myarry.Count;
               统计集合内元素的个数
               Console.WriteLine("总共有"+aa+"个元素");

        

        myarry.Clear();//将集合清空

                bool b = myarry.Contains(3);            

         判断是否有括号内的数据,返回的是bool值(True或者False)

             int bb = myarry.IndexOf(2);            

        int cc = myarry.LastIndexOf(2);            

        Console.WriteLine(bb);

        

        myarry.Sort();           

          自动进行排序,升序

               

        若需要降序排列,在自动排列之后,对整个集合进行反转指令            

        myarry.Reverse();       

                    

        ArrayList ar = new ArrayList();           

         ar = (ArrayList)myarry.Clone();     //集合的克隆

        

         foreach (object a in myarry)           

        {

      //从头到尾打印出集合中的所有元素                

        Console.WriteLine(a);           

       }

                Console.ReadLine();

          

     //集合中有元素是以object这个类型存在
                //object类是所有类的基类
                //小的数据类型不可以接收object类型的数据
                //object类型的变量可以接收任何类型的变量
                //string aaa = "2";
                //object bbb = aaa;
                //aaa不可以接收bbb的值

        

         Console.WriteLine(myarry[3]);
                 Console.ReadLine();

      

       Stack aa = new Stack();
                aa.Push(1);//向stack集合中添加数据
                aa.Push(2);
                aa.Push(3);
                aa.Push(4);
                aa.Push(5);

        //注意:Stack是没有索引的
                //Console.WriteLine(aa[0]);//是错误的

         Console.WriteLine(aa.Peek());//仅用来查看最后一位,不踢出

        //aa.Pop();//弹出,踢出最后一个进入集合的数据  

         //Console.WriteLine( aa.Pop());

               

        int aaa = aa.Count;//统计个数

               

         aa.Clear();//清空集合

  • 相关阅读:
    Delphi中Format与FormatDateTime函数详解
    常用的日期时间函数
    100m和1000m网线的常见制作方法
    Delphi cxGrid –--> RecordIndex out of Range
    局域网共享需要密码
    提高AdoQuery的速度
    string literals may have at most 255 elements
    001-project基本使用
    Java-idea-创建maven项目,部署项目,部署服务器,简单测试
    003-spring结合java类调用quartz
  • 原文地址:https://www.cnblogs.com/longhaijun/p/5719731.html
Copyright © 2011-2022 走看看