C# ArrayListd length problem solution

黄舟
Release: 2017-10-08 09:35:03
Original
2003 people have browsed it

C# Solving the length problem of ArrayListd

namespace ArrayListd的长度问题
{
   class Program
   {
       static void Main(string[] args)
       {  
           //需要的参数是object类型
           //alt+shift+F10添加引用using System.Collections;
           ArrayList list = new ArrayList();
           //count 表示集合中实际包含的元素个数
           //capity集合中可以包含的元素的个数
           //超过了包含的个数的时候,集合就会向内存中多申请开辟一倍的空间
           list.Add(2);
           list.Add(1);
           list.Add(2);
           list.Add(3);
           list.Add(4);
          // list.RemoveAt(0);//移除某个索引位置的元素
           list.Sort();//123456
          // list.Reverse();//654321
           list.TrimToSize();//如果加上这个,list.Capacity这个是实际的元素数,不是4,8,12了
            list.ToArray();
foreach (var item in list)
           {
               Console.WriteLine(item);
           }
// list.Clear();//经所有的元素清除完
            bool b=  list.Contains(1);//看看元素中是否包含某个元素  1
          Console.WriteLine(list.Count);//1-2
          Console.WriteLine(list.Capacity);//Capacity这个属性是,超过四个元素变成8,超过8变成12
          Console.WriteLine(b);
          Console.ReadKey();
}
   }
}
===================================================
namespace ArrayList练习
{
   class Program
   {
       static void Main(string[] args)
       {
         #region add.list()
//            //不是静态类,就可以创建一个对象
//            //集合:很多数据的集合
//            //集合的好处:长度任意改变,类型不固定
//            //数组的长度不可变,类型单一
//            ArrayList List = new ArrayList();
//            List.Add(0);//这个地方放什么都可以
//            List.Add(3.14);
//            List.Add("zhangsan ");
//            List.Add(true);
//            List.Add('c');

//            List.Add(new int[]{1,2,3,4,5});
//            Person p = new Person();
//            List.Add(p);//自定义类的对象放进去
//            //List.Add(list);
//            for (int i = 0; i < List.Count; i++)
//            {      //List[i]可以装换成person类型
//                if (List[i] is Person)
//                {
//                    //((Person)List[i]).say();
//                }
//                Console.WriteLine(List[i]);
//                else if (List[i] is int[])
//                {                       // 强装换成int[]类型
//                    for (int j = 0; j < ((int[])List[i]).Length; j++)
//                    {
//                        Console.WriteLine(((int[])List[i])[j]);
//                    }
//                }
//                else
//                {
//                    Console.WriteLine(List[i]);
//                }
//            }
//            Console.ReadKey();
#endregion
           ArrayList List = new ArrayList();
           //添加单个元素
           List.Add(1);
           List.Add(2);
           List.Add(6);
           List.Add(0);
          // List.Add("张三");
           //添加集合
           List.AddRange(new int[]{1,2,3,4,5,6,7});
           //记住在ArrayLi中List的长度是用Count基数的,不是Length
           //移除元素
           //List.Clear();//清空所有元素
           //List.Remove(1);//移除单个元素,括号里写谁就删除谁
           //List.RemoveAt(0);//根据下标来删除元素,这个1是下标1也就是zahngsan
          // List.RemoveRange(0,4);
           //还是根据下标开始删除括号里的意思是从第0个下标开始删除删除2个元素
           //后面是4,把前面的单个元素删除完毕后就开始删除数组里面的元素
           //List.Sort();//升续排序
           // List.Reverse();//反转
           //插入到要插入的元素后面,后面的插入的没有类型要求
           List.Insert(1, "我是插入的");
          //插入到指定位置索引
           List.InsertRange(1,new string[]{"李四,老五,赵六"});
           //判断是否包含某个指定的元素,用bool类型接收一下
           bool b = List.Contains("我是插入的");
            Console.WriteLine(b);
            if (!List.Contains("猪"))
            {
                List.Add("猪");
            }
            else
            {
                Console.WriteLine("ppp");
            }
for (int i = 0; i < List.Count; i++)
           {
               //输出也是输出每一个元素List[i]
               Console.WriteLine(List[i]);
           }
           Console.ReadKey();
       }


   }
   public class Person
   { 
    public static void say()
       {
           Console.WriteLine("我是人类");
       }
   }
}
Copy after login

The above is the detailed content of C# ArrayListd length problem solution. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!