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" );
         }
     }
}

 

本文转自ylbtech博客园博客,原文链接:http://www.cnblogs.com/ylbtech/archive/2012/08/27/2657434.html,如需转载请自行联系原作者

相关文章
|
30天前
|
C#
C#学习相关系列之数组---常用方法使用(二)
C#学习相关系列之数组---常用方法使用(二)
|
30天前
|
存储 C#
C#学习系列相关之数组(一)---数组的定义与使用
C#学习系列相关之数组(一)---数组的定义与使用
|
30天前
|
存储 人工智能 C#
【Unity 3D】C#中数组、集合、栈、队列、哈希表、字典的讲解(附测试代码)
【Unity 3D】C#中数组、集合、栈、队列、哈希表、字典的讲解(附测试代码)
46 0
|
8月前
|
开发框架 .NET C#
c#数组补充
c#数组的几个简单的补充
28 0
|
30天前
|
C#
C# 字节数组与INT16,float,double之间相互转换,字符数组与字符串相互转换,
C# 字节数组与INT16,float,double之间相互转换,字符数组与字符串相互转换,
92 1
|
30天前
|
存储 安全 Java
32.C#:ArrayList集合
32.C#:ArrayList集合
20 1
|
30天前
|
存储 C# C++
C# 笔记2 - 数组、集合与与文本文件处理
C# 笔记2 - 数组、集合与与文本文件处理
53 0
|
30天前
|
存储 数据可视化 C#
C# Break 和 Continue 语句以及数组详解
它被用于“跳出” switch 语句。 break 语句也可用于跳出循环。 以下示例在 i 等于 4 时跳出循环: 示例:
61 0
|
30天前
|
存储 C#
C#基础语法(数组和函数)
C#基础语法(数组和函数)
25 1
|
30天前
|
Java C# 索引
【从Java转C#】第六章:数组
【从Java转C#】第六章:数组