C#基础(16)——ArrayList

1、ArrayList集合

它不是静态类,所以可以new一个对象,创建集合对象:

ArrayList list = new ArrayList();

由于数组长度不可变,类型单一;
集合的好处:长度任意改变,类型随便;
对于数组,把命名空间打印出来:
这里写图片描述

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayList集合
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.Add(1);
            list.Add(3.141592653);
            list.Add(true);
            list.Add("zhang");
            list.Add(500m);
            list.Add(new int[] { 1, 2, 3, 4, 5, 6, 7 });
            list.Add(new Person());
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }
            Console.ReadKey();
        }
    }

    public class Person
    {
        public void PersonSay()
        {
            Console.WriteLine("This Is Person");
        }
    }
}

打印里面的所有内容:
这里写图片描述

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayList集合
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.Add(1);
            list.Add(3.141592653);
            list.Add(true);
            list.Add("zhang");
            list.Add(500m);
            list.Add(new int[] { 1, 2, 3, 4, 5, 6, 7 });
            list.Add(new Person());
            for (int i = 0; i < list.Count; i++)
            {
                //Console.WriteLine(list[i]);
                if (list[i] is Person)
                {
                    ((Person)list[i]).PersonSay();
                }
                else if (list[i] is int[])
                {
                    for (int j = 0; j < ((int[])list[i]).Length; j++)
                    {
                        Console.Write("{0}  ", ((int[])list[i])[j]);
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine(list[i]);
                }
            }
            Console.ReadKey();
        }
    }

    public class Person
    {
        public void PersonSay()
        {
            Console.WriteLine("This Is Person");
        }
    }
}

2、ArrayList元素的增加

代码简化,使用AddRange:
AddRange是添加集合的方法,Add是添加单个元素的方法。
这里写图片描述

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayList集合
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.AddRange(new string[] { "1", "2", "3" });
            list.AddRange(new int[] { 4, 5, 6, 7, 8 });
            for (int i = 0; i < list.Count; i++)
            {
                Console.Write("{0}  ",list[i]);
            }
            Console.ReadKey();
        }
    }

    public class Person
    {
        public void PersonSay()
        {
            Console.WriteLine("This Is Person");
        }
    }
}

3、ArrayList元素的插入

list.Insert(0, 0);插入单个元素
list.InsertRange(0, new int[] { 9, 10, 11 });插入集合

4、ArrayList元素的删除

list.Remove(8);删除单个元素
list.RemoveAt(0);根据索引删除
list.RemoveRange(0,3);根据下标移除一定范围的方法

5、ArrayList元素的清空

list.Clear();清空所以元素

6、ArrayList元素的排序

只有统一的类型才能排序

list.Sort();

7、ArrayList元素的反转

      list.Reverse();

8、是否包含

list.Contains(8);返回bool

9、集合长度问题

count实际上包含元素个数
capcity集合中可以包含的元素个数,内存申请开辟多一倍的空间 :4→8→16→32→64…..来保证集合长度够用

10、集合练习

求集合的平均值和总和:

这里写图片描述

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayList集合
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 89, 100 });
            int sum = 0;
            int max = int.MinValue;
            for (int i = 0; i < list.Count; i++)
            {
                if ((int)list[i] > max)
                {
                    max = (int)list[i];
                }
                sum += (int)list[i];
            }
            Console.WriteLine("总和:{0}\n均值:{1}\n最大值:{2}", sum,sum*1.0/list.Count,max);
            Console.ReadKey();
        }
    }


}

写一个长度为10 的集合,要求集合里的数字随机产生,并且互不相同:

这里写图片描述
方法1:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayList集合
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            {
                int rNumber = r.Next(0, 10);
                list.Add(rNumber);
                if (i > 0)
                {
                    for (int j = 0; j < i; j++)
                    {
                        if ((int)list[i] == (int)list[j])
                        {
                            rNumber = r.Next(0, 10);
                            list.RemoveAt(i);
                            list.Add(rNumber);
                            j = -1;//因为返回去时有j++就变成0了,单步调试可以发现
                        }
                    }

                }
            }

            for (int i = 0; i < list.Count; i++)
            {
                Console.Write("{0}  ",(int)list[i]);
            }
            Console.ReadKey();
        }
    }


}

方法2:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayList集合
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            {
                int rNumber = r.Next(0, 10);
                if (!list.Contains(rNumber))//不重复的
                {
                    list.Add(rNumber);
                }
                else//重复的
                {
                    i--;//重新来过
                }
            }

            for (int i = 0; i < list.Count; i++)
            {
                Console.Write("{0}  ", (int)list[i]);
            }
            Console.ReadKey();
        }
    }


}

方法3:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayList集合
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            {
                int rNumber = r.Next(0, 10);
                while (list.Contains(rNumber))//死循环
                {
                    rNumber = r.Next(0, 10);
                }
                list.Add(rNumber);
            }

            for (int i = 0; i < list.Count; i++)
            {
                Console.Write("{0}  ",(int)list[i]);
            }
            Console.ReadKey();
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值