002_数组ArrayList

作者: HMY轩园 | 来源:发表于2017-05-08 19:49 被阅读0次

    非泛型集合ArrayList:

    using System;
    using System.Collections.Generic;//泛型
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;//非泛型
    
    namespace demo
    {
        class example_05
        {
            static void Main()
            {
                ArrayList array = new ArrayList();
                array.Add(100);
                array.Add(86);
                int position = array.Add(77);
                Console.WriteLine(position);//2 
    
                int total = 0;//和
                double average = 0.0;//平均值
    
               foreach (int item in array)
                {
                    total += item;
                }
    
                average = total / array.Count;
    
                Console.WriteLine(total);
                Console.WriteLine(average);
    
                array.Insert(1, 99);//早索引为1的地方插入99
                array.Insert(3,80);
    
                Console.WriteLine(array.Count);//元素数量 5
                Console.WriteLine(array.Capacity);// 8
                array.Insert(1, 99);
                array.Insert(3, 80);
                array.Insert(1, 99);
                array.Insert(3, 80);
    
                Console.WriteLine(array.Count);//元素数量  9
                Console.WriteLine(array.Capacity);//数组默认容器大小,如果大于8时,自动扩充到16.
    
               if (array.Contains(54))//检查是否包含
               {
                   array.Remove(54);//移除元素
               }
                else
                {
                    Console.WriteLine("没有54");
                }
    
                array.RemoveAt(4);//移除索引处的元素
                Console.WriteLine(array.Count);
    
                array.Add(70);
               int pos = array.IndexOf(70);//查找元素的索引位置
               Console.WriteLine("pos:"+pos);
    }
    }
    }
    
     ArrayList names = new ArrayList();
                names.Add("zhandsan");
                names.Add("钱琪");
                names.Add("zhaoliu");
    
                foreach (Object name in names)
                {
                    Console.WriteLine(name);
                }
                Console.WriteLine();
    
                String[] newNames = new String[] { "qwe", "dsd" };
                ArrayList moreNames = new ArrayList();
                moreNames.Add("刘晓丽");
                moreNames.Add("临海");
                moreNames.Add("例如");
    
                names.InsertRange(0, newNames);//插入多个
                names.AddRange(moreNames);//多个加到末尾
                Console.WriteLine("添加了6个元素以后的内容");
                foreach (Object name in names)
                {
                    Console.WriteLine(name);
                }
                ArrayList someName = new ArrayList();
                someName = names.GetRange(2, 3);//获取索引2开始的3个元素
                foreach (Object name in someName)
                {
                    Console.WriteLine(name);
                }
                names.Clear();//清空
    

    相关文章

      网友评论

        本文标题:002_数组ArrayList

        本文链接:https://www.haomeiwen.com/subject/adkstxtx.html