C# ArrayList(数组列表)

专题图:ylbtech-.net编号:ylbtech  DotNet100010013

1,ArrayList(数组列表)

 Implements the IList interface using an array whose size is dynamically increased as required.

【提供一些方法,用于创建、处理、搜索数组并对数组进行排序,从而充当公共语言运行时中所有数组的基类。】

命名空间:  System.Collections
程序集:  mscorlib(在 mscorlib.dll 中)

2,Syntax(语法)

  

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class ArrayList : IList, ICollection, 
	IEnumerable, ICloneable

 

 3,备注:

ArrayList 可能并不总是提供特定任务的最佳性能。 有关这些类相对性能的讨论参见。 List<T> “性能注意事项”部分引用主题。

ArrayList 不保证是排序的。 在执行需要对 ArrayList 排序的操作(如 BinarySearch)之前,必须对 ArrayList 进行排序。

ArrayList 的容量是 ArrayList 可以包含的元素数。 随着向 ArrayList 中添加元素,容量通过重新分配按需自动增加。 可通过调用 TrimToSize 或通过显式设置 Capacity 属性减少容量。

对于非常大 ArrayList 对象,则在运行时环境 (ide) 中增加最大容量为 20亿在 64 位系统的元素通过设置 gcAllowVeryLargeObjects 配置元素的 enabled 属性设置为 true 。

可使用一个整数索引访问此集合中的元素。 此集合中的索引从零开始。

ArrayList 集合接受 null 作为有效值并且允许重复的元素。

不支持将多维数组用作 ArrayList 集合中的元素。


引用: http://msdn.microsoft.com/zh-cn/library/system.collections.arraylist.aspx

4,ArrayList【示例】

 

using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        /// <summary>
        /// ylb_menu:ArrayList(数组列表)
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {


            ArrayList al = new ArrayList(5);

            //添加
            al.Add("sunshine");
            al.Add("rain");
            al.Add("sun");
            al.Add("rain");

            //查找项
            if (al.Contains("sunshine"))
            {
                Console.WriteLine("数组包含“sunshine”");
            }

            //清空数组
            //al.Clear();

            Console.WriteLine("数组的个数" + al.Count);
            
            //查找根据值,得到元素索引位置
            //IndexOf
            Console.WriteLine(al.IndexOf("yuanbo"));
            Console.WriteLine(al.IndexOf("rain"));
            Console.WriteLine(al.IndexOf("rain", 0));
            Console.WriteLine(al.IndexOf("rain", 2, 2));
            //LastIndexOf
            Console.WriteLine(al.LastIndexOf("rain"));

            //遍历
            Graph(al);
            //插入一个项
            al.Insert(2, "snoopy");
            Graph(al);
            
            //反转
            al.Reverse();
            Graph(al);

            //排序 
            al.Sort();
            Graph(al);

            //移除
            al.Remove("sunshine");  //根据项
            Graph(al);  
            al.RemoveAt(2); //下标
            Graph(al);

            把数组转化为字符串
            //Console.WriteLine(al.ToString());

            //Array arr=al.ToArray();
            //for (int i = 0;i< arr.Length; i++)
            //{
            //    Console.Write(arr.ToString());
            //}
            string[] strs=new string[al.Count];

            //付给字符串数组
            al.CopyTo(strs);

            foreach(string s in strs)
            {
                Console.Write(s + "\t");
            }
        }
        /// <summary>
        /// 遍历数组
        /// </summary>
        /// <param name="al"></param>
        static void Graph(ArrayList al)
        {
            foreach (string a in al)
            {
                Console.Write(a+"\t");
            }
            Console.Write("\n");
        }
    }
}

 

warn作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

最终目标

 代码的国际化标准示例 ylb,tech”,最大程度地规范软件编程开发统一,优质, 高效,易学,为建设软件强国(中国)而努力。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值