0

0

C#基础知识整理 基础知识(16) IList接口——非泛型

黄舟

黄舟

发布时间:2017-02-11 13:44:42

|

1491人浏览过

|

来源于php中文网

原创

了解了icollection接口、迭代以及泛型集合,下面再详细了解一下ilist接口。
通过msdn可以看到ilist接口有两种:

元素为object类型的ilist接口,可以放不同类型的对象引用;
ilist泛型接口,只能存放指定类型的对象引用。
其实,ilist和ilist也称之为向量,特点是可以动态的改变集合的长度,无需确定集合的初始长度,集合会随着存放数据的数量自动变化。
可以看到ilist和ilist的继承关系:

[ComVisibleAttribute(true)]
public interface IList : ICollection, IEnumerable

public interface IList : ICollection, 
 IEnumerable, IEnumerable

现在再返回去看下,IList和IList的区别,看如下代码,ArrayList是继承IList的,List继承IList

   public class IListClass
    {
        void test()
        {
            TestClass1 c1 = null;

            ArrayList arryList = new ArrayList();

            arryList.Add(c1);

            List list = new List();

            list.Add(c1);

            //取值
            TestClass1 getC1Array = arryList[0] as TestClass1;//必须要一次强制转换

            TestClass1 getC1List = list[0];//不需要转换,所谓泛型
        }
    }

    public class TestClass1
    {
    }

这下就比较明白了。
一、IList接口概述
 ILis接口从ICollection接口继承,具备以下特性,
 Count属性——获取集合元素个数;
 GetEnumerator方法——可以迭代;
 CopyTo方法——将指定元素复制到另一个数组中;
 Clear方法——清空整个集合。
 IList新增特性,
 索引器属性——根据索引访问集合中任意元素;
 Add方法——向集合末尾添加元素;
 Insert方法——向集合指定位置插入元素;
 Remove方法——移除指定元素;(包括RemoveAt)
 Contains方法——判断对象是否在集合中;
 IndexOf方法——查找指定对象在集合中的索引位置。
 另外,IList接口集合按照顺序存放元素,不改变元素存放顺序。
2、算法
  向量集合和数组一样,具备随即访问的特点。即无论访问向量集合的任何一个单元,所需的访问时间是完全相同的。在向量类中,实际上依然使用普通数组来记录集合数据,向量类使用了一些算法技巧,让整个类对外表现不同于普通数组的重要特点:可以动态改变数组长度。具体算法如下:
  在内部数组足够长的情况下,直接进行添加和插入操作,在内部数组长度不足的情况下,按照内部数组长度增加2倍作为新的数组的长度,然后进行数据搬移(即把就数组数组移到新数组中)。向量在分配元素存储空间时,会多分配一些冗余空间,尽量减少内存分配次数。在数据删除时,并不改变内部数组长度,仅仅是使用被删除数据之后的数据覆盖被删除的数据。
  不过向量每次分配空间时都多分配一些冗余空间,会造成内存的压力,因此在程序中应该尽量避免集中的次数繁多的内存分配。
 三、实现类
 IList和IList的实现类,分别是ArrayList类和List类。
 ArrayList类处于System.Collection命名空间下;
 List类处于System.Collection.Specialized命名空间下。
 四、实现代码(非泛型)
 

/// 
    /// 实现IList,非泛型
    /// 
    public class ArrayList : IList
    {
        /// 
        /// 迭代
        /// 
        public struct Enumertor : IEnumerator
        {
            /// 
            /// 迭代索引
            /// 
            private int index;

            /// 
            /// 迭代器所属的向量类对象的引用
            /// 
            private ArrayList arrayList;

            /// 
            /// 构造函数
            /// 
            /// 迭代器所属的集合类
            public Enumertor(ArrayList arrayList)
            {
                this.arrayList = arrayList;

                this.index = -1;
            }

            /// 
            /// 获取当前对象,根据index的值返回向量对应的对象引用
            /// 
            public object Current
            {
                get 
                { 
                    return arrayList[index]; 
                }
            }

            /// 
            /// 将迭代器指向下一个数据位置,通过改变index的值,加1或减1
            /// 
            /// 
            public bool MoveNext()
            {
                if (this.index < arrayList.Count)
                {
                    ++this.index;
                }

                return this.index < arrayList.Count;
            }

            /// 
            /// 迭代器回到起始位置,将index置为-1
            /// 
            public void Reset()
            {
                this.index = -1;
            }
        }

        /// 
        /// 保存集合的数组
        /// 
        private object[] array = new object[1];

        /// 
        /// 当前集合的长度
        /// 
        private int count;

        /// 
        /// 默认构造函数
        /// 
        public ArrayList()
        {

        }

        /// 
        /// 参数构造函数,通过参数指定内部数组长度,减少重新分配空间
        /// 
        /// 
        public ArrayList(int capacity)
        {
            if (capacity < 0)
            {
                throw new Exception();
            }

            if (capacity == 0)
            {
                capacity = 1;
            }

            this.array = new object[capacity];

            this.count = 0;
        }

        public int Count
        {
            get
            {
                return this.count;//该属性只读
            }
        }

        /// 
        /// 集合实际使用长度
        /// 
        public int Capacity
        {
            get
            {
                return this.array.Length;
            }
        }

        /// 
        /// 是否固定大小
        /// 
        public bool IsFixedSize
        {
            get
            {
                return false;
            }
        }

        /// 
        /// 是否只读集合
        /// 
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }
        /// 
        /// 是否同步,即是否支持多线程访问
        /// 
        public bool IsSynchronized
        {
            get
            {
                return false;
            }
        }
        /// 
        /// 同步对象
        /// 
        public object SyncRoot
        {
            get
            {
                return null;
            }
        }

        /// 
        /// 当array长度不足时,重新分配新的长度足够的数组
        /// 
        /// 
        private object[] GetNewArray()
        {
            return new object[(this.array.Length + 1) * 2];
        }

        public int Add(object value)
        {
            int newCount = this.count + 1;

            if (this.array.Length < newCount)//长度不足
            {
                object[] newArray = GetNewArray();

                Array.Copy(this.array, newArray, this.count);

                this.array = newArray;//重新引用,指向新数组
            }

            //增加新元素
            this.array[this.count] = value;

            this.count = newCount;

            //返回新元素的索引位置
            return this.count - 1;
        }

        /// 
        /// 索引器属性,按索引返回向量中的某一项
        /// 
        /// 
        /// 
        public object this[int index]
        {
            get
            {
                if (index < 0 || index >= this.count)
                {
                    throw new Exception();
                }

                return this.array[index];
            }

            set
            {
                if (index < 0 || index >= this.count)
                {
                    throw new Exception();
                }

                this.array[index] = value;
            }
        }

        /// 
        /// 删除集合中的元素
        /// 
        /// 
        /// 
        public void RemoveRange(int index, int count)
        {
            if (index < 0)
            {
                throw new Exception();
            }

            int removeIndex = index + count;//计算集合中最后一个被删元素的索引

            if (count < 0 || removeIndex > this.count)
            {
                throw new Exception();
            }

            //删除其实是将要删除元素之后的所有元素拷贝到要删除元素的位置覆盖掉
            Array.Copy(this.array, index + 1, this.array, index + count - 1, this.count - removeIndex);

            //重新设置集合长度
            this.count -= count;
        }

        /// 
        /// 查找对应的数组项,实际是遍历查找
        /// 
        /// 
        /// 
        public int IndexOf(object value)
        {
            int index = 0;

            if (value == null)
            {
                while (index < this.count)
                {
                    if (this.array[index] == null)
                    {
                        return index;
                    }

                    ++index;
                }
            }
            else
            {
                while (index < this.count)
                {
                    if (this.array[index].Equals(value))
                    {
                        return index;
                    }

                    ++index;
                }
            }

            return -1;
        }

        /// 
        /// 从集合中删除指定元素
        /// 
        /// 
        public void Remove(object value)
        {
            int index = this.IndexOf(value);

            if (index >= 0)
            {
                this.RemoveRange(index, 1);
            }
        }

        /// 
        /// 从集合中删除指定位置的元素
        /// 
        /// 
        public void RemoveAt(int index)
        {
            RemoveRange(index, 1);
        }

        /// 
        /// 获取最后一个元素的引用后删除最后一个元素
        /// 
        /// 
        public object PopBack()
        {
            object obj = this.array[this.count - 1];

            RemoveAt(this.count - 1);

            return obj;
        }

        /// 
        /// 获取第一个元素引用并删除第一个元素
        /// 
        /// 
        public object PropFront()
        {
            object obj = this.array[0];

            RemoveAt(0);

            return obj;
        }

        /// 
        /// 插入元素
        /// 
        /// 
        /// 
        public void Insert(int index, object value)
        {
            if (index >= this.count)
            {
                throw new Exception();
            }
            //插入元素当空间不足时也是声明新的2倍长度数组,并拷贝旧数据。
            //插入数据原理是,将指定位置后的数据全部后移,再将新数据放在指定位置。

            int newCount = this.count + 1;

            if (this.array.Length < newCount)
            {
                object[] newArray = GetNewArray();

                Array.Copy(this.array, newArray, index);

                this.array = newArray;
            }

            Array.Copy(this.array, index, this.array, index + 1, this.count - index);

            this.array[index] = value;

            this.count = newCount;
        }

        /// 
        /// 查看当前集合是否包含指定对象
        /// 
        /// 
        /// 
        public bool Contains(object value)
        {
            return this.IndexOf(value) >= 0;
        }

        /// 
        /// 将集合的长度改变为实际长度
        /// 
        public void TrimToSize()
        {
            //为了消除Add和Insert时增加的冗余,原理是新生成一个和实际长度相同的数组,然后将值全部移过来。
            if (this.array.Length > this.count)
            {
                object[] newArray = null;

                if (this.count > 0)
                {
                    newArray = new object[this.count];

                    Array.Copy(this.array, newArray, this.count);
                }
                else
                {
                    newArray = new object[1];
                }

                this.array = newArray;
            }
        }

        /// 
        /// 清空集合
        /// 
        public void Clear()
        {
            this.count = 0;
        }
        /// 
        /// 获取集合的迭代器
        /// 
        /// 
        public IEnumerator GetEnumerator()
        {
            Enumertor enumerator = new Enumertor(this);

            return enumerator;
        }

        /// 
        /// 转移集合元素
        /// 
        /// 
        /// 
        public void CopyTo(Array targetArray, int index)
        {
            Array.Copy(this.array, 0, targetArray, index, this.count);
        }
    }


调用测试:

 

Videoleap
Videoleap

Videoleap是一个一体化的视频编辑平台

下载
static void Main(string[] args)
        {
            //调用测试

            ArrayList myArrayList = new ArrayList();

            myArrayList.Add(40);

            myArrayList.Add(80);

            myArrayList.Add("Hello");

            //使用for循环遍历
            for (int i = 0; i < myArrayList.Count; i++)
            {
                Console.WriteLine(myArrayList[i]);
            }

            Console.WriteLine("---------------------");

            //使用迭代循环
            foreach (object obj in myArrayList)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("---------------------");

            myArrayList.Insert(1, "Insert");

            foreach (object obj in myArrayList)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("---------------------");

            myArrayList.Remove("Insert");

            foreach (object obj in myArrayList)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("---------------------");

            myArrayList.RemoveAt(1);

            foreach (object obj in myArrayList)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("---------------------");

            myArrayList.Clear();

            foreach (object obj in myArrayList)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("---------------------");

            Random rand = new Random();

            for (int i = 0; i < 10; i++)
            {
                myArrayList.Add(rand.Next(10));
            }

            foreach (object obj in myArrayList)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("---------------------");

            Console.WriteLine("集合是否包含为1的元素 ? " + (myArrayList.Contains(0) ? "包含" : "不包含"));

            Console.WriteLine("元素1的位置   " + myArrayList.IndexOf(1));

            Console.ReadLine();
        }

结果:

以上就是的内容,更多相关内容请关注PHP中文网(www.php.cn)!

 

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
C++ 设计模式与软件架构
C++ 设计模式与软件架构

本专题深入讲解 C++ 中的常见设计模式与架构优化,包括单例模式、工厂模式、观察者模式、策略模式、命令模式等,结合实际案例展示如何在 C++ 项目中应用这些模式提升代码可维护性与扩展性。通过案例分析,帮助开发者掌握 如何运用设计模式构建高质量的软件架构,提升系统的灵活性与可扩展性。

14

2026.01.30

c++ 字符串格式化
c++ 字符串格式化

本专题整合了c++字符串格式化用法、输出技巧、实践等等内容,阅读专题下面的文章了解更多详细内容。

9

2026.01.30

java 字符串格式化
java 字符串格式化

本专题整合了java如何进行字符串格式化相关教程、使用解析、方法详解等等内容。阅读专题下面的文章了解更多详细教程。

12

2026.01.30

python 字符串格式化
python 字符串格式化

本专题整合了python字符串格式化教程、实践、方法、进阶等等相关内容,阅读专题下面的文章了解更多详细操作。

4

2026.01.30

java入门学习合集
java入门学习合集

本专题整合了java入门学习指南、初学者项目实战、入门到精通等等内容,阅读专题下面的文章了解更多详细学习方法。

20

2026.01.29

java配置环境变量教程合集
java配置环境变量教程合集

本专题整合了java配置环境变量设置、步骤、安装jdk、避免冲突等等相关内容,阅读专题下面的文章了解更多详细操作。

18

2026.01.29

java成品学习网站推荐大全
java成品学习网站推荐大全

本专题整合了java成品网站、在线成品网站源码、源码入口等等相关内容,阅读专题下面的文章了解更多详细推荐内容。

19

2026.01.29

Java字符串处理使用教程合集
Java字符串处理使用教程合集

本专题整合了Java字符串截取、处理、使用、实战等等教程内容,阅读专题下面的文章了解详细操作教程。

3

2026.01.29

Java空对象相关教程合集
Java空对象相关教程合集

本专题整合了Java空对象相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.29

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
【web前端】Node.js快速入门
【web前端】Node.js快速入门

共16课时 | 2万人学习

Go语言实战之 GraphQL
Go语言实战之 GraphQL

共10课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号