如何使用Java ArrayList

Java ArrayList 是通用的可调整大小的数组。它提供了其他语言通常期望的大多数设施。其中包括:使用索引访问元素,添加,删除和更新元素,动态调整大小,对元素进行迭代等。这些操作中的大多数已针对一般用途进行了专门调整。

有其他一些特殊类型的“数组"(实现 List 接口的类,属于技术类)。其中包括:

  • LinkedList 支持在中间索引处快速插入和删除。
  • Vector ArrayList类似,但它是同步的,并且适合于多线程应用程序的 ArrayList
  • Stack 支持模拟最后一个操作的操作,先进先出列表。它扩展了 Vector ,因此是同步的。
  • 这些特殊类不在本文讨论范围之内。但是,您将学习如何设置和使用通用Java ArrayList。

    创建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]

    要查找数组列表中有多少项,请使用方法 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 的特定类型。这是一种从一堆项目构造一个 List 的方法(使用 Arrays.asList())并将其添加到 ArrayList

    List items = Arrays.asList("pear", "cherry");alist.addAll(items);System.out.println(alist);# prints[apple, banana, cantaloupe, grapes, orange, pear, cherry]

    当然,您可以在此处指定索引作为第一个参数,以添加从该索引开始的项。

    访问项

    一旦项已添加到数组列表后,我们如何再次访问它们?

    如果您知道该项的索引,则可以使用 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

    如果该项目不在数组列表中怎么办?当找不到该项时, 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"

    此代码使用Java增强功能Java 1.5中引入的For-Loop。在此之前,您可以使用迭代器遍历项目。当您需要在迭代过程中删除元素时,也可以使用迭代器,如下例所示。 (请注意,我们会复制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]

    删除项目

    现在让我们看看如何从数组列表。如果您知道项目的索引(也许您使用了上述的 indexOf()),则可以对索引使用 remove()方法。它返回已删除的元素。

    String fruit = alist.remove(2);System.out.println("Removed element at 2: " + fruit);# printsRemoved element at 2: cantaloupe

    您还可以指定元素以删除列表中元素的 first 出现。如果找到并删除了元素,该方法将返回 true

    fruit = "grapes";System.out.println("Remove " +fruit+ " from the list? " + alist.remove(fruit));# printsRemove grapes from the list? true

    标签: