通过实例学C#之ArrayList

介绍

        ArrayList对象可以容纳若干个具有相同类型的对象,那有人说,这和数组有什么区别呢。其区别大概可以分为以下几点:

1.数组效率较高,但其容量固定,而且没办法动态改变。

2.ArrayList容量可以动态增长,但它的效率,没有数组高。

所以建议,如果能确定容纳对象数量的话,那么优先使用数组,否则,使用ArrayList为佳。


构造函数

ArrayList()

        返回一个capacity属性为0的实例,但capacity为0,不代表其不能内部添加对象,而是会随着对象的增加,而动态改变其capacity属性。

static void Main(string[] args)
{
    ArrayList al= new ArrayList();
    Console.WriteLine(al.Capacity);
    Console.ReadKey();
}

运行结果:
0

ArrayList(ICollection)

        利用一个数组来创建ArrayList实例,实例的Capacity属性为数组的大小。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };

    ArrayList al= new ArrayList(arrayInt);
    Console.WriteLine(al.Capacity);
    Console.ReadKey();
}

运行结果:
5

ArrayList(Int32)

        使用一个整形参数来创建一个ArrayList对象,其Capacity等于参数值。

static void Main(string[] args)
{
    ArrayList al= new ArrayList(10);
    Console.WriteLine(al.Capacity);
    Console.ReadKey();
}

运行结果:
10

常用属性

Capacity

        ArrayList对象的容量大小。


Count

        ArrayList对象包含的元素数量。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };

    ArrayList al= new ArrayList(arrayInt);
    Console.WriteLine(al.Count);
    Console.ReadKey();
}

运行结果:
5

Item[int32]

        可以通过索引获取指定index的元素值。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };

    ArrayList al= new ArrayList(arrayInt);
    Console.WriteLine(al[0]);
    Console.ReadKey();
}

运行结果:
1

常用方法

Add(Object)

        在ArrayList实例的结尾添加一个元素。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };

    ArrayList al= new ArrayList(arrayInt);
    al.Add(6);

    foreach(int i in al)
    {
        Console.WriteLine(i);
    }
    Console.ReadKey();
}

运行结果:
1
2
3
4
5
6

AddRange(ICollection)

        在ArrayList实例的末尾添加一个数组。

 static void Main(string[] args)
 {
     int[] arrayInt = { 1, 2, 3, 4, 5 };
     ArrayList al= new ArrayList(arrayInt);

     int[] arrayAdd = { 10, 11, 12 };
     al.AddRange(arrayAdd);

     foreach(int i in al)
     {
         Console.WriteLine(i);
     }
     Console.ReadKey();
 }

运行结果:
1
2
3
4
5
10
11
12

BinarySearch(Object value)

        寻找参数value出现在ArrayList实例中的位置,如果实例中不含有value这元素,那么返回一个负数。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);

    int idx=al.BinarySearch(3);
    Console.WriteLine("元素3所在的位置是:"+idx);

    idx=al.BinarySearch(100);
    Console.WriteLine("元素100所在的位置是:" + idx);

    Console.ReadKey();
}

运行结果:
元素3所在的位置是:2
元素100所在的位置是:-6

Clear()

        清除ArrayList对象中的所有元素。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);

    al.Clear();
    Console.WriteLine("al的元素有:");
    foreach (int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
al的元素有:

Contains(Object item)

        判断ArrayList实例中是否含有item元素,如果有,返回true,否则,返回false。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);

    Console.WriteLine("al中是否含有元素5?:"+al.Contains(5));
    Console.WriteLine("al中是否含有元素100?:" + al.Contains(100));

    Console.ReadKey();
}

运行结果:
al中是否含有元素5?:True
al中是否含有元素100?:False

CopyTo(int index, Array array, int arrayIndex, int count)

        把ArrayList实例中从index开始的count个元素,复制到array中从arrayIndex开始的元素。

 static void Main(string[] args)
 {
     int[] arrayInt = { 1, 2, 3, 4, 5 };
     ArrayList al= new ArrayList(arrayInt);

     int[] array = new int[3];       //创建一个长度为3的int数组
     al.CopyTo(1, array, 0, 3);

     foreach(int i in array)
     {
         Console.WriteLine(i);
     }

     Console.ReadKey();
 }

运行结果:
2
3
4

FixedSize(ArrayList)

        输入一个ArrayList对象,返回该对象的一个具有固定长度的复制,如果对复制执行Add()操作,则会报错。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);
    
    ArrayList fixAl=ArrayList.FixedSize(al);
    fixAl.Add(6);
    foreach(int i in fixAl)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:


GetRange (int index, int count)

        由ArrayList实例中从index起,共count个元素组成一个新的ArrayList进行输出。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);
    
    ArrayList al2=al.GetRange(1, 3);
    foreach(int i in al2)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
2
3
4

IndexOf (object value)

        返回value值在ArrayLIst实例中的index。

 static void Main(string[] args)
 {
     int[] arrayInt = { 1, 2, 3, 4, 5 };
     ArrayList al= new ArrayList(arrayInt);

     Console.WriteLine("元素5的index为:"+al.IndexOf(5));

     Console.ReadKey();
 }

运行结果:
元素5的index为:4

Insert (int index, object value);

        在ArrayList实例中index位置插入新元素value。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);
    al.Insert(1, 100);
    Console.WriteLine("al的元素有:");

    foreach (int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
al的元素有:
1
100
2
3
4
5

InsertRange (int index, ICollection c);

        在ArrayList实例中的index位置插入数组c。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);

    int[] insertInt = { 100, 101, 102 };
    al.InsertRange(1, insertInt);
    Console.WriteLine("al的元素有:");

    foreach (int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
al的元素有:
1
100
101
102
2
3
4
5

LastIndexOf (object  value)

        获取元素value最后一次出现的index值。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 ,1};
    ArrayList al= new ArrayList(arrayInt);
    Console.WriteLine("元素1最后一次出现的位置是:"+al.LastIndexOf(1));

    Console.ReadKey();
}

运行结果:
元素1最后一次出现的位置是:5

ReadOnly (ArrayList list)

        输入一个ArrayList参数,返回一个元素与参数一样的只读ArrayList对象。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 ,1};
    ArrayList al= new ArrayList(arrayInt);
    Console.WriteLine("al的readonly属性为:" + al.IsReadOnly);

    ArrayList readOnlyAl = ArrayList.ReadOnly(al);
    Console.WriteLine("readOnlyAl的readonly属性为:"+readOnlyAl.IsReadOnly);

    Console.ReadKey();
}

运行结果:
al的readonly属性为:False
readOnlyAl的readonly属性为:True

Remove (object obj)       

        清除ArrayList实例中的指定元素obj。如果obj多次出现,那么只清除第一个出现的obj元素。

 static void Main(string[] args)
 {
     int[] arrayInt = { 1, 2, 3, 4, 5 ,1};
     ArrayList al= new ArrayList(arrayInt);
     
     al.Remove(1);
     foreach(int i in al)
     {
         Console.WriteLine(i);
     }

     Console.ReadKey();
 }


运行结果:
2
3
4
5
1

RemoveAt(int index)

        清除index位置上的元素。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 ,1};
    ArrayList al= new ArrayList(arrayInt);
    
    al.RemoveAt(1);
    foreach(int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
1
3
4
5
1

RemoveRange (int index, int count)

        清除ArrayList对象中起始位置为index,长度为count的区域里的元素。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 ,1};
    ArrayList al= new ArrayList(arrayInt);
    
    al.RemoveRange(1,3);
    foreach(int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
1
5
1

Repeat (object value, int count)

        使用count个value元素,组成一个新的ArrayList对象。

static void Main(string[] args)
{
    ArrayList al = ArrayList.Repeat(100, 5);

    foreach(int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
100
100
100
100
100

Reverse ()

        将ArrayList对象的所有元素进行反向排序。

 static void Main(string[] args)
 {
     int[] arrayInt = { 1, 2, 3, 4, 5, 1 };
     ArrayList al = new ArrayList(arrayInt);

     al.Reverse();

     foreach (int i in al)
     {
         Console.WriteLine(i);
     }

     Console.ReadKey();
 }

运行结果:
1
5
4
3
2
1

SetRange (int index, ICollection c)

        将ArrayList实例中从index开始的元素,替换为数组c的元素。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5, 1 };
    ArrayList al = new ArrayList(arrayInt);

    int[] replaceInt = { 100, 101, 102 };
    al.SetRange(1, replaceInt);

    foreach (int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
1
100
101
102
5
1

Sort ()

        将ArrayList实例中的元素进行排序。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5, 1 };
    ArrayList al = new ArrayList(arrayInt);

    al.Sort();

    foreach (int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
1
1
2
3
4
5

ToArray ()

        将ArrayList对象转换成一个object数组。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5, 1 };
    ArrayList al = new ArrayList(arrayInt);

    object[] array=al.ToArray();

    foreach (int i in array)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
1
2
3
4
5
1

TrimToSize ()

        将ArrayList对象的capacity属性设置为其实际含有的元素数量。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5, 1 };
    ArrayList al = new ArrayList(arrayInt);
    al.Capacity = 10;
    Console.WriteLine("al的capacity为:"+al.Capacity);

    al.TrimToSize();
    Console.WriteLine("al的capacity为:" + al.Capacity);

    Console.ReadKey();
}

运行结果:
al的capacity为:10
al的capacity为:6

  • 17
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值