JavaArrayList是一个通用的可调整大小的数组。它提供了其他语言中数组通常需要的大部分功能。这些操作包括:使用索引访问元素、添加、删除和更新元素、动态重新调整大小、对元素进行迭代等。这些操作中的大多数都经过了专门调整,以用于一般用途。
还有一些其他类型的“数组”(实现列表接口的类,这是技术性的),它们是用于特殊目的的。其中包括:
这些特殊类不在本文的范围内。但是,您将学习如何设置和使用通用JavaArrayList。
创建ArrayList很简单。可以使用无参数构造函数创建空ArrayList。这里我们创建一个空的arraylist来保存字符串。
ArrayList alist = new ArrayList();如果碰巧知道数组列表将包含多少项,则可以指定初始容量。这个初始容量只是内存分配的一个提示——arraylist并不局限于容纳指定数量的项。如果您知道并指定了初始容量,您可能会在性能上得到一点改进。
ArrayList alist = new ArrayList(20);填充arraylist相当容易。只需使用add()方法将单个项添加到arraylist的末尾。举个例子:
ArrayList alist = new ArrayList();alist.add("apple");alist.add("banana");alist.add("cantaloupe");alist.add("orange");System.out.println(alist);# prints[apple, banana, cantaloupe, orange]要查找arraylist中有多少项,请使用size()方法。
System.out.println("Number of elements in the arraylist: " + alist.size());# printsNumber of elements in the arraylist: 4要在任意索引处添加项吗?将索引指定为第一个参数,并在该索引处添加项:
alist.add(3, "grapes");System.out.println(alist);# prints[apple, banana, cantaloupe, grapes, orange]您也可以在Java集合层次结构中添加来自任何集合的项。ArrayList是一个名为List的特定类型。下面是一种从一组项目构造列表的方法(使用数组.asList())并将其添加到ArrayList。
List items = Arrays.asList("pear", "cherry");alist.addAll(items);System.out.println(alist);# prints[apple, banana, cantaloupe, grapes, orange, pear, cherry]当然,您可以在这里指定索引作为第一个参数,以添加从该索引开始的项。
一旦项目被添加到arraylist,我们如何再次访问它们?
如果知道项的索引,可以使用get()方法检索该索引处的元素。
String item = alist.get(2);System.out.println("Item at index 2 is: " + item);# printsItem at index 2 is: cantaloupe如果你不知道项目的索引怎么办?可以使用indexOf()检查该项是否存在于数组中,并使用返回的索引检索该项。
System.out.println(alist);int index = alist.indexOf("orange");if ( index < 0 ) System.out.println("Item \"orange\" not found");else System.out.println("Item \"orange\" found at index " + index);# prints[apple, banana, cantaloupe, grapes, orange, pear, cherry]Item "orange" found at index 4如果项目不在arraylist中怎么办?当找不到项时,indexOf()方法返回-1。
index = alist.indexOf("grape");if ( index < 0 ) System.out.println("Item \"grape\" not found");else System.out.println("Item \"grape\" found at index " + index);# printsItem "grape" not found当然,ArrayList最常见的用法是遍历元素。这可以通过多种方式实现。我们在这里展示几个简单的例子。
下面是迭代arraylist并提取项以进行某种处理的最简单方法。
for (String fruit : alist) { System.out.println("Found fruit \"" + fruit + "\"");}# printsFound fruit "apple"Found fruit "banana"Found fruit "cantaloupe"Found fruit "grapes"Found fruit "orange"Found fruit "pear"Found fruit "cherry"此代码使用Java1.5中引入的Java增强For循环。在此之前,您可以使用迭代器对项进行迭代。当您需要在迭代过程中删除元素时,也可以使用迭代器,如下例所示。(请注意,我们**了一个arraylist的副本并处理了该副本。)
ArrayList blist = new ArrayList(alist);for (Iterator iter = blist.iterator() ; iter.hasNext() ; ) { String fruit = iter.next(); if ( fruit.startsWith("c") ) iter.remove(); else System.out.println("Keeping \"" + fruit + "\"");}# printsKeeping "apple"Keeping "banana"Keeping "grapes"Keeping "orange"Keeping "pear"一旦添加了项目,我们需要一种方法来替换不需要的项目。可以使用set()方法和索引来完成这一点。
alist.set(5, "pineapple");System.out.println(alist);# prints[apple, banana, cantaloupe, grapes, orange, pineapple, cherry]现在让我们看看如何从arraylist中删除项。如果您知道该项的索引(可能使用了上面描述的indexOf()),则可以对索引使用remove()方法。它返回删除的元素。
String fruit = alist.remove(2);System.out.println("Removed element at 2: " + fruit);# printsRemoved element at 2: cantaloupe您还可以指定要删除列表中第一个出现的元素的元素。如果找到并删除了元素,则该方法返回true。
fruit = "grapes";System.out.println("Remove " +fruit+ " from the list? " + alist.remove(fruit));# printsRemove grapes from the list? true你是如何在你的项目中使用ArrayList的?你遇到了哪些特殊问题?请在下面的评论中告诉我们。
...象自动转换为其相应的原语类型的过程称为取消装箱。像ArrayLists这样的集合使用包装类,因为它们存储对象。 什么是java中的基元类型(primitive type in java)? 原始数据类型是Java编程语言提供的预定义数据类型。有八种基本类型。...
...元素2,那么元素1将位于元素2之前。 图01:列表和设置 ArrayList,LinkedList,Vector是一些实现List的类。在ArrayList中,访问元素的速度很快,但**和删除速度较低。ArrayList不是线程安全的。从多个线程访问同一个ArrayList可能不会得到...
关键区别–arraylist与linkedlist 集合对于存储数据很有用。在普通数组中,数组大小是固定的。有时需要创建可以根据需要增长的阵列。Java等编程语言有集合。它是一个包含一组类和接口的框架。它充当一组元素的容器。集合...
... public class Hello { public static void main(String[] args) { ArrayList list = new ArrayList(); }} ...
... 实现这个接口的类必须实现所有这些方法。ArrayList类使用阵列支持的存储系统实现此接口。可宣布如下: ...
...main" java.lang.IndexOutOfBoundsException: Index: 8, Size: 5 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at sample.sample1.main(sample1.java:24) ...
ArrayList与Vector 向量实现的数组可以在运行时在添加或删除某些元素时进行增长/收缩。使用整数索引访问其元素。两个字段–容量和容量增量,是矢量存储管理的特征。它实现了四个接口:*列表*随机访问*可克隆*可串行化接口 ...
...是他们,有什么区别(they, and what’s the difference)? List和ArrayList是Java和C语言中的一些代码,允许您设置和调用参数。在坚果壳里。困惑的?我也是。下一段时间你需要一些知识。 通常,List是一个用于ArrayList或LinkedList的接口。列...
什么是数组和数组列表(array and arraylist)? Array和ArrayList都是Java程序中常用的基于索引的数据结构。从概念上讲,ArrayList在内部是由数组支持的,然而,理解两者之间的区别是成为优秀Java开发人员的关键。这是最基本的一步,尤...
...的Java时代就已经出现了。就像Vector是更现代、更高级的ArrayList的同步对应物一样,Hashtable也是HashMap的同步对应物。但是,类不能同步,所以当我们说Hashtable是一个同步映射时,意味着类的关键方法是同步的。 虽然两者实际上是...