如何使用java arraylist

JavaArrayList是一个通用的可调整大小的数组。它提供了其他语言中数组通常需要的大部分功能。在本文中,您将学习如何设置和使用arraylist。...

JavaArrayList是一个通用的可调整大小的数组。它提供了其他语言中数组通常需要的大部分功能。这些操作包括:使用索引访问元素、添加、删除和更新元素、动态重新调整大小、对元素进行迭代等。这些操作中的大多数都经过了专门调整,以用于一般用途。

howto-java-arraylist

还有一些其他类型的“数组”(实现列表接口的类,这是技术性的),它们是用于特殊目的的。其中包括:

  • LinkedList支持在中间索引处快速**和删除。
  • Vector类似于ArrayList,但是同步的,适合于多线程应用程序中替代ArrayList。
  • 堆栈支持模拟后进先出列表的操作。它扩展了向量,因此是同步的。

ArrayList Class Hierarchy from Java Collecti***

这些特殊类不在本文的范围内。但是,您将学习如何设置和使用通用JavaArrayList。

创建arraylist

创建ArrayList很简单。可以使用无参数构造函数创建空ArrayList。这里我们创建一个空的arraylist来保存字符串。

ArrayList alist = new ArrayList();

如果碰巧知道数组列表将包含多少项,则可以指定初始容量。这个初始容量只是内存分配的一个提示——arraylist并不局限于容纳指定数量的项。如果您知道并指定了初始容量,您可能会在性能上得到一点改进。

ArrayList alist = new ArrayList(20);

A new ArrayList with empty slots

填充arraylist

在末尾添加项目

填充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

Adding Items to an ArrayList at the end.

在指定索引处添加项

要在任意索引处添加项吗?将索引指定为第一个参数,并在该索引处添加项:

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的?你遇到了哪些特殊问题?请在下面的评论中告诉我们。

  • 发表于 2021-03-13 20:41
  • 阅读 ( 204 )
  • 分类:编程

你可能感兴趣的文章

包装类(wrapper class)和java中的基元类型(primitive type in java)的区别

...象自动转换为其相应的原语类型的过程称为取消装箱。像ArrayLists这样的集合使用包装类,因为它们存储对象。 什么是java中的基元类型(primitive type in java)? 原始数据类型是Java编程语言提供的预定义数据类型。有八种基本类型。...

  • 发布于 2020-10-19 06:27
  • 阅读 ( 736 )

列表(list)和设置(set)的区别

...元素2,那么元素1将位于元素2之前。 图01:列表和设置 ArrayList,LinkedList,Vector是一些实现List的类。在ArrayList中,访问元素的速度很快,但**和删除速度较低。ArrayList不是线程安全的。从多个线程访问同一个ArrayList可能不会得到...

  • 发布于 2020-10-19 09:09
  • 阅读 ( 204 )

数组表(arraylist)和双链表(linkedlist)的区别

关键区别–arraylist与linkedlist 集合对于存储数据很有用。在普通数组中,数组大小是固定的。有时需要创建可以根据需要增长的阵列。Java等编程语言有集合。它是一个包含一组类和接口的框架。它充当一组元素的容器。集合...

  • 发布于 2020-10-19 11:43
  • 阅读 ( 229 )

初学者必备的8个eclipse键盘快捷键

... public class Hello { public static void main(String[] args) { ArrayList list = new ArrayList(); }} ...

  • 发布于 2021-03-11 23:36
  • 阅读 ( 334 )

入门时应学习的10个核心java概念

... 实现这个接口的类必须实现所有这些方法。ArrayList类使用阵列支持的存储系统实现此接口。可宣布如下: ...

  • 发布于 2021-03-13 05:33
  • 阅读 ( 244 )

java异常:您处理的对吗?

...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) ...

  • 发布于 2021-03-13 09:11
  • 阅读 ( 255 )

数组表(arraylist)和矢量(vector)的区别

ArrayList与Vector 向量实现的数组可以在运行时在添加或删除某些元素时进行增长/收缩。使用整数索引访问其元素。两个字段–容量和容量增量,是矢量存储管理的特征。它实现了四个接口:*列表*随机访问*可克隆*可串行化接口 ...

  • 发布于 2021-06-23 18:41
  • 阅读 ( 236 )

列表(list)和数组表(arraylist)的区别

...是他们,有什么区别(they, and what’s the difference)? List和ArrayList是Java和C语言中的一些代码,允许您设置和调用参数。在坚果壳里。困惑的?我也是。下一段时间你需要一些知识。 通常,List是一个用于ArrayList或LinkedList的接口。列...

  • 发布于 2021-06-24 03:08
  • 阅读 ( 215 )

数组(array)和数组表(arraylist)的区别

什么是数组和数组列表(array and arraylist)? Array和ArrayList都是Java程序中常用的基于索引的数据结构。从概念上讲,ArrayList在内部是由数组支持的,然而,理解两者之间的区别是成为优秀Java开发人员的关键。这是最基本的一步,尤...

  • 发布于 2021-06-25 03:21
  • 阅读 ( 653 )

散列表(hashmap)和哈希表(hashtable)的区别

...的Java时代就已经出现了。就像Vector是更现代、更高级的ArrayList的同步对应物一样,Hashtable也是HashMap的同步对应物。但是,类不能同步,所以当我们说Hashtable是一个同步映射时,意味着类的关键方法是同步的。 虽然两者实际上是...

  • 发布于 2021-06-25 20:14
  • 阅读 ( 246 )
深海深澜_
深海深澜_

0 篇文章

相关推荐