ArrayList用法整理
System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。
一.优点
1、支持自动改变大小的功能
2、可以灵活的插入元素
3、可以灵活的删除元素
二.局限性
跟一般的数组比起来,速度上差些
三.添加元素
1.public virtual int Add(objectvalue);
将对象添加到ArrayList的结尾处
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
内容为abcde
2.public virtual void Insert(intindex,objectvalue);
将元素插入ArrayList的指定索引处
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Insert(0,"aa");
结果为aaabcde
3.public virtual void InsertRange(intindex,ICollectionc);
将集合中的某个元素插入ArrayList的指定索引处
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
ArrayList list2 = new ArrayList();
list2.Add("tt");
list2.Add("ttt");
aList.InsertRange(2,list2);
结果为abtttttcde
四.删除
1. public virtual void Remove(objectobj);
从ArrayList中移除特定对象的第一个匹配项,注意是第一个
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Remove("a");
结果为bcde
2. public virtual void RemoveAt(intindex);
移除ArrayList的指定索引处的元素
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveAt(0);
结果为bcde
3.public virtual void RemoveRange(intindex,intcount);
从ArrayList中移除一定范围的元素。Index表示索引,count表示从索引处开始的数目
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveRange(1,3);
结果为ae
4.public virtual void Clear();
从ArrayList中移除所有元素。
五.排序
a) public virtual void Sort();
对ArrayList或它的一部分中的元素进行排序。
ArrayList aList = new ArrayList();
aList.Add("e");
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
DropDownList1.DataSource = aList; //DropDownListDropDownList1;
DropDownList1.DataBind();
结果为eabcd
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Sort();//排序
DropDownList1.DataSource = aList;//DropDownListDropDownList1;
DropDownList1.DataBind();
结果为abcde
b) public virtual void Reverse();
将ArrayList或它的一部分中元素的顺序反转。
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Reverse();//反转
DropDownList1.DataSource = aList;//DropDownListDropDownList1;
DropDownList1.DataBind();
结果为edcba
六.查找
a) public virtual int IndexOf(object);
b) public virtual int IndexOf(object,int);
c) public virtual int IndexOf(object,int,int);
返回ArrayList或它的一部分中某个值的第一个匹配项的从零开始的索引。没找到返回-1。
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
intnIndex = aList.IndexOf(“a”);//1
nIndex = aList.IndexOf(“p”);//没找到,-1
d) public virtual int LastIndexOf(object);
e) public virtual int LastIndexOf(object,int);
f) public virtual int LastIndexOf(object,int,int);
返回ArrayList或它的一部分中某个值的最后一个匹配项的从零开始的索引。
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("a");//同0
aList.Add("d");
aList.Add("e");
intnIndex = aList.LastIndexOf("a");//值为2而不是0
g) public virtual bool Contains(objectitem);
确定某个元素是否在ArrayList中。包含返回true,否则返回false
七.其他
1.public virtual int Capacity{get;set;}
获取或设置ArrayList可包含的元素数。
2.public virtual int Count{get;}
获取ArrayList中实际包含的元素数。 Capacity是ArrayList可以存储的元素数。Count是ArrayList中实际包含的元素数。Capacity总是大于或等于Count。如果在添加元素时,Count超过Capacity,则该列表的容量会通过自动重新分配内部数组加倍。 如果Capacity的值显式设置,则内部数组也需要重新分配以容纳指定的容量。如果Capacity被显式设置为0,则公共语言运行库将其设置为默认容量。默认容量为16。 在调用Clear后,Count为0,而此时Capacity切是默认容量16,而不是0
3.public virtual void TrimToSize();
将容量设置为ArrayList中元素的实际数量。 如果不向列表中添加新元素,则此方法可用于最小化列表的内存系统开销。 若要完全清除列表中的所有元素,请在调用TrimToSize之前调用Clear方法。截去空ArrayList会将ArrayList的容量设置为默认容量,而不是零。
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");//Count=5,Capacity=16,
aList.TrimToSize();//Count=Capacity=5;
ArrayList用法整理的更多相关文章
- Google Guava 库用法整理<转>
参考: http://codemunchies.com/2009/10/beautiful-code-with-google-collections-guava-and-static-imports- ...
- JAVA中ArrayList用法
JAVA中ArrayList用法 2011-07-20 15:02:03| 分类: 计算机专业 | 标签:java arraylist用法 |举报|字号 订阅 Java学习过程中做题时 ...
- Spring JdbcTemplate用法整理
Spring JdbcTemplate用法整理: xml: <?xml version="1.0" encoding="UTF-8"?> <b ...
- linq用法整理
linq用法整理 普通查询 var highScores = from student in students where student.ExamScores[exam] > score se ...
- linux学习:特殊符号,数学运算,图像与数组与部分终端命令用法整理
指令:let.expr.array.convert.tput.date.read.md5.ln.apt.系统信息 一:特殊符号用法整理 系统变量 $# 是传给脚本的参数个数 $0 是脚本本身的名字 $ ...
- #ifndef#define#endif的用法(整理)
[转] #ifndef#define#endif的用法(整理) 原作者:icwk 文件中的#ifndef 头件的中的#ifndef,这是一个很关键的东西.比如你有两个C文件,这两个C文件都in ...
- MySQL中使用SHOW PROFILE命令分析性能的用法整理(配合explain效果更好,可以作为优化周期性检查)
这篇文章主要介绍了MySQL中使用show profile命令分析性能的用法整理,show profiles是数据库性能优化的常用命令,需要的朋友可以参考下 show profile是由Jerem ...
- ArrayList用法详解与源码分析
说明 此文章分两部分,1.ArrayList用法.2.源码分析.先用法后分析是为了以后忘了查阅起来方便-- ArrayList 基本用法 1.创建ArrayList对象 //创建默认容量的数组列表(默 ...
- Android spannableStringBuilder用法整理
Android spannableStringBuilder用法整理 分类: Android开发2013-11-29 10:58 5009人阅读 评论(0) 收藏 举报 Androidspannabl ...
随机推荐
- ServiceFabric极简文档-5.0 Service Fabric有状态与无状态
Service Fabric 应用程序方案 2017/08/14 作者 Edward Chen Jack Zeng Azure Service Fabric提供了一个可靠而灵活的平台,可用于编写和运行 ...
- SpringBoot工程热部署
SpringBoot工程热部署 1.在pom文件中添加热部署依赖 <!-- 热部署配置 --> <dependency> <groupId>org.springfr ...
- 异步编程之Async,Await和ConfigureAwait的关系
在.NET Framework 4.5中,async / await关键字已添加到该版本中,简化多线程操作,以使异步编程更易于使用.为了最大化利用资源而不挂起UI,你应该尽可能地尝试使用异步编程.虽然 ...
- c语言进阶13-线性表之顺序表
一. ACM算法:顺序表的查找 顺序表的查找指获取顺序表的第i个元素.对于线性表的顺序存储结构来说,如果我们要实现获取元素的操作(GetElem),即将线性表L中的第i个位置元素值返回.就程序而言,只 ...
- jQuery甘特图/日程图/横道图/插件
基于JQ的一款灵活高效,支持自定义拓展的甘特图/日程图插件 支持月/周/小时等显示方式 支持拖动改变时间 展开与收起 添加/删除,刷新 节假日高亮 clicked,dblClicked,changed ...
- 回顾二分与bfs(或者说是递推)和简单模拟
今天,阳光正好,适合敲代码,诸事皆宜. 先来两道简单的模拟题. 第一道 机器翻译 输出为5. 代码思路:很明显需要用到队列来存单词,在建立一个bool数组来存储队列中有没有这个单词,需不需要向外界查询 ...
- C#命名规范(简述)
命名空间,类,事件,接口,常量,属性,方法使用Pascal命名,即首字母大写 参数,变量(类字段)使用camel命名法,即首字母小写. Pascal 方式--所有单词第一个字母大写,其他字母小写. ...
- iframe插入视频自动播放代码
<iframe marginwidth=0 marginheight=0 src='http://www.wsview.com/yzplayerAction!play2.action?autoP ...
- 前端jQuery学习(一)
把最近学习的前端框架jQuery整理一下.你可能听说过jQuery,因为他是JavaScript世界中使用最广泛的一个库. 江湖传言,全世界大约有80~90%的网站直接或间接地使用了jQuery.鉴于 ...
- ACM线性基学习笔记
https://www.cnblogs.com/31415926535x/p/11260897.html 概述 最近的几场多校出现了好几次线性基的题目,,会想起之前在尝试西安区域赛的一道区间异或和最大 ...