JAVA中ArrayList用法

2011-07-20 15:02:03|  分类: 计算机专业 |  标签:java  arraylist用法  |举报|字号 订阅

 
 
Java学习过程中做题时,用到ArrayList,在网上寻找到的学习资料。

  摘自:   

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

一.优点

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

二.局限性

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

三.添加元素

1.publicvirtualintAdd(objectvalue);

将对象添加到ArrayList的结尾处

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

2.publicvirtualvoidInsert(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.publicvirtualvoidInsertRange(intindex,ICollectionc);

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

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

结果为abtttttcde

四.删除

1. publicvirtualvoidRemove(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. 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。

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) publicvirtualintLastIndexOf(object);
e) publicvirtualintLastIndexOf(object,int);
f) publicvirtualintLastIndexOf(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) 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的容量设置为默认容量,而不是零。

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;

JAVA中ArrayList用法的更多相关文章

  1. java中ArrayList 、LinkList区别

    转自:http://blog.csdn.net/wuchuanpingstone/article/details/6678653 个人建议:以下这篇文章,是从例子说明的方式,解释ArrayList.L ...

  2. Java中ArrayList与LinkedList的区别

    Java中ArrayList与LinkedList的区别 一般大家都知道ArrayList和LinkedList的区别: 1. ArrayList的实现是基于数组,LinkedList的实现是基于双向 ...

  3. this在java中的用法

    this在java中的用法 1.使用this关键字引用成员变量 作用:解决成员变量与参数或局部变量命名冲突的问题 public class Dog { String name; public Dog( ...

  4. Java中arraylist和linkedlist源代码分析与性能比較

    Java中arraylist和linkedlist源代码分析与性能比較 1,简单介绍 在java开发中比較经常使用的数据结构是arraylist和linkedlist,本文主要从源代码角度分析arra ...

  5. java中ArrayList 和 LinkedList 有什么区别

    转: java中ArrayList 和 LinkedList 有什么区别 ArrayList和LinkedList都实现了List接口,有以下的不同点:1.ArrayList是基于索引的数据接口,它的 ...

  6. Java中ArrayList类详解

    1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和ILis ...

  7. Java中Annotation用法

    其他还可以参考的地址 https://www.cnblogs.com/skywang12345/p/3344137.html Annotation Annotation其实是代码里的特殊标记,这些标记 ...

  8. Java中ArrayList的自我实现

    对于ArrayList相比大家都很熟悉,它是java中最常用的集合之一.下面就给出它的自我实现的java代码. 需要说明的一点是,它是基于数组创建的.所以它在内存中是顺序存储,对于查找十分的方便. p ...

  9. Java中instanceof用法

    java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例. 用法:resu ...

随机推荐

  1. [转]使用 HttpClient 和 HtmlParser 实现简易爬虫

    http://www.ibm.com/developerworks/cn/opensource/os-cn-crawler/ http://blog.csdn.net/dancen/article/d ...

  2. UIApplication及UIWindow

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  3. 150 Opening ASCII mode data connection. FTP连接的PASV和PORT方式

    FTP连接服务器时出现 150 Opening ASCII mode data connection.一般是客户端设置PORT 主动方式连接造成的.切换成PASV 被动模式后一般能连接上. 一.什么是 ...

  4. 第一课 opengl简介

    1. 什么是opengl: opengl是图形硬件的一种软件接口. 2. opengl对场景中的图像进行渲染时所执行的主要图形操作 1)根据几何图元创建形状,从而建立物体的数学描述. 2)在三维空间中 ...

  5. RC振荡器与有源滤波器1

    自我总结,有错误欢迎指出! 一.振荡器原理: 对于正反馈,有如下公式: 讨论: 1.当A*F(jw)=1时 Af→∞(物理意义:自激振荡): 2.在f0上,F(jw)相移为0或2n∏:(因此,在f0处 ...

  6. vimdiff vimmerge 配置及使用

    1 Set up vimdiff The vimdiff as a merge tool will display several buffers to show the yours/theirs/o ...

  7. No.012 Integer to Roman

    12. Integer to Roman Total Accepted: 71315 Total Submissions: 176625 Difficulty: Medium Given an int ...

  8. oracle中,行转列函数wm_concat()结果有长度限制,重写该函数解决

    --Type CREATE OR REPLACE TYPE zh_concat_im AUTHID CURRENT_USER AS OBJECT ( CURR_STR clob, STATIC FUN ...

  9. ionic cordova social media sharing plugin

    https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin.git 从上面的连接下载 插件 1.肯定是要加入 下面的那个文件的吖 从 ...

  10. HTML转义字符集合

    readme:这次可以不readme了,因为这个是我copy过来的~ ISO Latin-1字符集:  — 制表符Horizontal tab  — 换行Line feed  — 回车Carriage ...