通过实例学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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/627.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ros1中python3包调用自定义.py文件

ros中python包相互import不成功问题 问题解决办法 问题 在ros工程中,运行python文件难以直接import自己写的py文件,相互之间无法import,但是在python3虚拟环境python *.py文件就可以正常运行! 注意这里还有个问题,我…

❤️‍FlyFlow工作流周更来咯~~

FlyFlow 借鉴了钉钉与飞书的界面设计理念,致力于打造一款用户友好、快速上手的工作流程工具。相较于传统的基于 BPMN.js 的工作流引擎,我们提供的解决方案显著简化了操作逻辑,使得用户能够在极短的时间内构建定制化的业务流程,即便…

前端近7天,近半个月,近1个月,近1年的日期处理

前端如何获取近7天,近1年的日期进行查询? methods:{//近7天getRangeDate(ranges) {let nowDays new Date();let getYear nowDays.getFullYear();let getMonth nowDays.getMonth() 1;let getDate nowDays.getDate();let nd new Date();nd nd.valueOf();nd nd - ranges…

记录汇川:五个ST案例

起保停: 简单数学教学: 数据查找: 按钮检测: 数据堆栈:

【k8s】:kubectl 命令设置简写启用自动补全功能

【k8s】:kubectl 命令设置简写&启用自动补全功能 1、设置kubectl命令简写2、启用kubectl自动补全功能💖The Begin💖点点关注,收藏不迷路💖 Kubernetes(K8s)是一个强大的容器编排平台,而kubectl则是与之交互的命令行工具。尽管Kubernetes提供了强大的功能,但有时…

wiringpi库的应用 -- sg90 定时器 oled

sg 90舵机: 接线: VCC -- 红 GND -- 地 信号线 -- 黄 -- pwm 定时器: 先玩定时器: sg90 需要的pwm波需要定时器输出,so我们得先来玩一下定时器 分析:实现定时器,通过itimerval结构体以及函数setitimer产生的信号,系统…

python装饰器系列教程(1)

若为了与用户交互,有如下代码 def messageOne():print("今天天气是晴转多云")def messageTwo():print("今天的空气质量为优")messageOne() messageTwo()现需在每条提示信息之前加上一条关于客户来自中国的信息,可改写为 def messa…

快手本地生活服务商系统怎么操作?

当下,抖音和快手两大短视频巨头都已开始布局本地生活服务,想要在这一板块争得一席之地。而这也很多普通人看到了机遇,选择成为抖音和快手的本地生活服务商,通过将商家引进平台,并向其提供代运营服务,而成功…

深入探讨虚拟现实中的新型安全威胁:“盗梦攻击”及其防御策略

随着虚拟现实(VR)技术的飞速发展,用户体验达到了前所未有的沉浸水平,但也暴露在一系列新的安全威胁之下。本文着重介绍了近期出现的一种高度隐秘且影响深远的攻击手段——“盗梦攻击”。这一概念由芝加哥大学的研究人员提出&#…

前端打包webpack vite

起步 | webpack 中文文档 | webpack中文文档 | webpack中文网 npm run build 1webpack: mkdir webpack-demo cd webpack-demo npm init -y npm install webpack webpack-cli --save-dev vite : 快速上手 | Vue.js

介绍TCP协议标志位

TCP协议中的控制位(Flags)是TCP头部中的6个标志位,用于控制TCP连接的建立、维护和终止过程,以及在数据传输中的一些特定行为。以下是对每个标志位的详细介绍: SYN (Synchronize): 功能:用于建立…

【Entity Framework】闲话EF中批量配置

【Entity Framework】闲话EF中批量配置 文章目录 【Entity Framework】闲话EF中批量配置一、概述二、OnModelCreating中的批量配置元数据API的缺点 三、预先约定配置忽略类型默认类型映射预先约定配置的限制约定添加新约定替换现有约定约定实现注意事项 四、何时使用每种方法进…

游戏登录界面制作

登录界面制作 1.导入模块和初始化窗口 import subprocessimport tkinter as tkimport picklefrom tkinter import messageboxwindow tk.Tk()window.title(Welcome)window.geometry(450x300) 导入必要的模块,并初始化了主窗口window,设置了窗口的标题和…

【技巧】Leetcode 169. 多数元素【简单】

多数元素 给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 示例 : 输入:nums [2,2,1,1,1,2,2] 输出&a…

day22 java多线程 线程安全问题解决方案

线程安全问题 [面试题]继承Thread和实现Runnable有什么区别? 1.实现接口和继承类 - 实现接口更灵活因为可以多实现。 2.线程安全 同步代码块 : 继承Thread : 锁不可以是this 实现Runnable : 锁可以是this 同步方法 继承Thread : 同步方法要使用静态同步…

修改taro-ui-vue3的tabs组件源码增加数字标签

需求:taro-ui-vue3的tabs组件上增加数字标记 步骤一:node_modules文件夹下找到taro-ui-vue3/lib/tabs/index.js 把173行的这一段替换成下面这段,然后写上样式 default: () > item.number ? [h(View, {class: at-tabs__item_in}, {defau…

Unity导出package

C#代码导出后为一个dll,原有的不同平台的库不变。 以下操作均在build PC 平台下操作。 1.在要导出的文件夹下建assembly definition (Any platform) 2.将项目文件夹下的\Library\ScriptAssemblies中的相应assembly definition的dll复制到要导出的文件夹下 3.在uni…

gstreamer pad cap的协商

这是在大模型中获取的答案,有点意思。 在 GStreamer 中,大多数时候 pad 协商是通过框架自动完成的,并且不需要手动干预。但是,了解如何在代码里面执行 pad 协商是很有帮助的,这在创建自定义元素或更细致地控制数据流时…

关于Pwn的一些文章

关于Pwn的一些文章 仅仅是本人为了方便查阅资料而已 1、保护机制: https://www.zhihu.com/question/464671097/answer/3257122786https://blog.csdn.net/m0_71081503/article/details/127732602 2、DA常用快捷键及其作用 https://blog.csdn.net/weixin_4574396…

基于Java+SpringBoot+Vue前后端分离仓库管理系统

基于JavaSpringBootVue前后端分离仓库管理系统 🍅 作者主页 央顺技术团队 🍅 欢迎点赞 👍 收藏 ⭐留言 📝 🍅 文末获取源码联系方式 📝 🍅 查看下方微信号获取联系方式 承接各种定制系统 &#…