仰天一笑

昨日不悔,今日勿失,明日莫忧! —徐羽

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  230 随笔 :: 27 文章 :: 812 评论 :: 41 引用

System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。

一.优点

1。支持自动改变大小的功能
2。可以灵活的插入元素
3。可以灵活的删除元素

二.局限性

跟一般的数组比起来,速度上差些

三.添加元素

1.publicvirtualintAdd(objectvalue);

将对象添加到ArrayList的结尾处

ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
内容为abcde

2.publicvirtualvoidInsert(intindex,objectvalue);

将元素插入ArrayList的指定索引处

ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Insert(0,"aa");

结果为aaabcde

3.publicvirtualvoidInsertRange(intindex,ICollectionc);

将集合中的某个元素插入ArrayList的指定索引处

ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
ArrayListlist2=newArrayList();
list2.Add("tt");
list2.Add("ttt");
aList.InsertRange(2,list2);

结果为abtttttcde

四.删除

a)publicvirtualvoidRemove(objectobj);

从ArrayList中移除特定对象的第一个匹配项,注意是第一个

ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Remove("a");

结果为bcde

2.publicvirtualvoidRemoveAt(intindex);

移除ArrayList的指定索引处的元素

aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveAt(0);

结果为bcde

3.publicvirtualvoidRemoveRange(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.publicvirtualvoidClear();

从ArrayList中移除所有元素。

五.排序

a)publicvirtualvoidSort();

对ArrayList或它的一部分中的元素进行排序。

ArrayListaList=newArrayList();
aList.Add("e");
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
DropDownList1.DataSource=aList;//DropDownListDropDownList1;
DropDownList1.DataBind();

结果为eabcd

ArrayListaList=newArrayList();
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)publicvirtualvoidReverse();

将ArrayList或它的一部分中元素的顺序反转。

ArrayListaList=newArrayList();
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)publicvirtualintIndexOf(object);
b)publicvirtualintIndexOf(object,int);
c)publicvirtualintIndexOf(object,int,int);

返回ArrayList或它的一部分中某个值的第一个匹配项的从零开始的索引。没找到返回-1。

ArrayListaList=newArrayList();
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)publicvirtualintLastIndexOf(object);
e)publicvirtualintLastIndexOf(object,int);
f)publicvirtualintLastIndexOf(object,int,int);

返回ArrayList或它的一部分中某个值的最后一个匹配项的从零开始的索引。

ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("a");//同0
aList.Add("d");
aList.Add("e");
intnIndex=aList.LastIndexOf("a");//值为2而不是0

g)publicvirtualboolContains(objectitem);

确定某个元素是否在ArrayList中。包含返回true,否则返回false

七.其他

1.publicvirtualintCapacity{get;set;}

获取或设置ArrayList可包含的元素数。

2.publicvirtualintCount{get;}

获取ArrayList中实际包含的元素数。
Capacity是ArrayList可以存储的元素数。Count是ArrayList中实际包含的元素数。Capacity总是大于或等于Count。如果在添加元素时,Count超过Capacity,则该列表的容量会通过自动重新分配内部数组加倍。
如果Capacity的值显式设置,则内部数组也需要重新分配以容纳指定的容量。如果Capacity被显式设置为0,则公共语言运行库将其设置为默认容量。默认容量为16。
在调用Clear后,Count为0,而此时Capacity切是默认容量16,而不是0

3.publicvirtualvoidTrimToSize();

将容量设置为ArrayList中元素的实际数量。
如果不向列表中添加新元素,则此方法可用于最小化列表的内存系统开销。
若要完全清除列表中的所有元素,请在调用TrimToSize之前调用Clear方法。截去空ArrayList会将ArrayList的容量设置为默认容量,而不是零。

ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");//Count=5,Capacity=16,
aList.TrimToSize();//Count=Capacity=5;

posted on 2006-09-15 15:57 仰天一笑 阅读(22565) 评论(34)  编辑 收藏 网摘

评论

#1楼  2006-11-16 12:56 netguid      
正在使用arrayList,非常不错
  回复  引用  查看    

#2楼  2007-03-05 15:00 apple [未注册用户]
good! thanks!
  回复  引用    

有没有用arraylist变量做为参数传递,太耗内存的问题??
或者有没有替换方法
  回复  引用    

#4楼  2007-04-14 16:08 fewmud0 [未注册用户]
如果是先
class c1
{
private aa;
private bb;
}
arraryList al =new arraryList();
public void AddWIfo(c1 c)
{
this.al.Add(c);

}
请问那怎么查arraryList中的元素呢?
  回复  引用    

#5楼  2007-04-23 09:40 小妖 [未注册用户]
up up up
  回复  引用    

谢谢,写得非常具体!
  回复  引用    

#7楼  2007-07-03 10:35 aa [未注册用户]
good
  回复  引用    

#8楼  2007-07-08 16:05 刘仰丰 [未注册用户]
谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
  回复  引用    

#9楼  2007-07-09 11:23 lindazimu [未注册用户]
非常感谢!
但如果要获得List的所有的值或者某一个值该如何获取呢?
  回复  引用    

#10楼  2007-07-12 12:57 sky [未注册用户]
r
  回复  引用    

#11楼  2007-07-19 10:43 lilin [未注册用户]
不错不错
写的太详细了
非常感谢!



  回复  引用    

不错,我去试试~~
  回复  引用    

#13楼  2007-08-07 15:55 java [未注册用户]
thank you!!!!
  回复  引用    

#14楼  2007-08-13 13:59 moses [未注册用户]
楼主要注意点编程规范啊!
这代码看起来有点累,不过写得很好,感谢ing!
  回复  引用    

#15楼  2007-08-21 15:20 谢谢 [未注册用户]
写得非常具体! 呵呵! 顶一下
  回复  引用    

remove(object o) 从此列表中移除指定元素的单个实例(如果存在),此操作是可选的。
remove(int index) 移除此列表中指定位置上的元素。
removeRange(int fromIndex, int toIndex) 移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。
  回复  引用    

#17楼  2007-09-24 16:29 merry [未注册用户]
请问ArrayList 在哪个头文件? 谢谢!
  回复  引用    

#18楼  2007-10-20 15:16 77 [未注册用户]
hao
  回复  引用    

#19楼  2007-11-14 13:12 暗暗 [未注册用户]
谢谢 楼主给出XML代码
  回复  引用    

很好,谢谢你的资源
  回复  引用    

正在学习JAVA, 谢谢
  回复  引用    

#22楼  2007-11-27 09:44 zdd [未注册用户]
很具体
  回复  引用    

这是C#的吧?哪有JAVA?
  回复  引用    

#24楼  2007-12-09 13:17 arraylist [未注册用户]
ArrayList 为什么也可以直接加 int 类型的数据?
  回复  引用    

#25楼  2007-12-21 18:27 东风破 [未注册用户]
很不错感觉
  回复  引用    

无意中看到,又学了一些东西。谢谢!!!
  回复  引用    

#27楼  2008-01-11 09:40 程胜 [未注册用户]
因为ArrayList为类聚集 而所有对像都可以看作是类 只要是类 都能放入ArrayList里面 下面是我写的一个系统里用到ArrayList的地方 list为ArrayList类型
for(int c=0;i<list.size()&&c<40;c++){
for(int g=0;g<d;g++){
System.out.print((String)list.get(i)+" ");
p[c][g]=(String)list.get(i);
i++;
if(i==list.size()){
break;
}
}
System.out.println(" ");
}
  回复  引用    

好,不错不错
  回复  引用    

#29楼  2008-05-06 16:16 卢灶来 [未注册用户]
很好,有没有专门介绍java的这些常用类的书啊,介绍下呀!!
  回复  引用    

#30楼  2008-05-07 11:02 rewr [未注册用户]
些的好复杂啊 。

真麻烦。
  回复  引用    

很好,谢了
  回复  引用    

#32楼  2008-05-22 20:33 林荫道 [未注册用户]
不错不错。。。。写的虽然比较简单,,,,不过给人的感觉比较好懂~~~~~~
  回复  引用    

#33楼  2008-06-20 10:45 斌哥      
HAO
  回复  引用  查看    

#34楼  2008-06-21 09:12 langchao [未注册用户]
非常好
  回复  引用    


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接: