In this tutorial, we will learn about the Java ArrayList class. We will learn about different ArrayList operations and methods with the help of examples.
The ArrayList
class is an implementation of the List
interface that allows us to create resizable-arrays.
ArrayList类是List接口的实现,允许我们创建可调整大小的数组
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8N0oz5jx-1592847119269)(https://cdn.programiz.com/sites/tutorial2program/files/java-arraylist-class.png)]
Java Array Vs ArrayList
In Java, we need to declare the size of an array before we can use it. Once the size of an array is declared, it’s hard to change it.
在Java中,我们需要先声明数组的大小,然后才能使用它。一旦声明了数组的大小,就很难更改它。
To handle this issue, we can use the ArrayList
class. The ArrayList
class present in the java.util
package allows us to create resizable arrays.
要解决此问题,我们可以使用ArrayList类。java.util包中存在的ArrayList类允许我们创建可调整大小的数组。
Unlike arrays, array lists (objects of the ArrayList
class) can automatically adjust its capacity when we add or remove elements from it. Hence, array lists are also known as dynamic arrays.
与数组不同,当我们向数组列表添加或删除元素时,数组列表(ArrayList类的对象)可以自动调整容量。因此,数组列表也称为动态数组。
Creating an ArrayList
Here is how we can create array lists in Java:
ArrayList<Type> arrayList= new ArrayList<>();
Here, Type indicates the type of an array list. For example,
// create Integer type arraylist
ArrayList<Integer> arrayList = new ArrayList<>();
// create String type arraylist
ArrayList<String> arrayList = new ArrayList<>();
In the above program, we have used Integer
and String
. Here, Integer
is the corresponding wrapper class of the int
type.
在这里,Integer是int类型的相应包装器类
A wrapper class is a class that wraps a primitive data type. For example, the Integer
class wraps the int
type, the Float
class wraps the float
type, etc.
包装器类是包装原始数据类型的类。例如,Integer类包装int类型,Float类包装float类型。
Note: We can not create array lists of primitive data types like int
, float
, char
, etc. Instead, we have to use their corresponding wrapper class.
我们不能创建原始数据类型(如int, float, char等)的数组列表。相反,我们必须使用它们对应的包装器类。
In the case of strings, String
is a class and doesn’t have a wrapper class. Hence, we have used String
as it is.
对于字符串,String是一个类,没有包装类。因此,按原样使用String
We can also create array lists using the List
interface. It’s because the ArrayList
class implements the List
interface.
还可以使用List接口创建数组列表。这是因为ArrayList类实现了List接口
List<String> list = new ArrayList<>();
Methods of ArrayList
ArrayList
provides various methods that allow us to perform array list operations.
ArrayList提供了各种方法,使我们能够执行数组列表操作
Add Elements to an ArrayList
1. Using the add() method
To add a single element to the array list, we use the add()
method. For example,
要将单个元素添加到数组列表中,使用add()方法
package com.programiz.arraylist;
import java.util.ArrayList;
public class AddElements {
public static void main(String[] args){
ArrayList<String> animals = new ArrayList<>();
// Add elements
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("ArrayList: " + animals);
}
}
Output
ArrayList: [Dog, Cat, Horse]
2. Using index number
We can also add elements to an array list using indexes. For example,
还可使用索引将元素添加到数组列表中
package com.programiz.arraylist;
import java.util.ArrayList;
public class IndexNumber {
public static void main(String[] args){
ArrayList<String> animals = new ArrayList<>();
// Add elements
animals.add(0,"Dog");
animals.add(1,"Cat");
animals.add(2,"Horse");
System.out.println("ArrayList: " + animals);
}
}
Output
ArrayList: [Dog, Cat, Horse]
3. Add elements of an array list to another array list
将数组列表的元素添加到另一个数组列表
To add all the elements of an array list to a new array list, we use the addAll()
method. For example,
要将数组列表的所有元素添加到新数组列表中,使用addAll()方法
package com.programiz.arraylist;
import java.util.ArrayList;
puublic class AddAll {
public static void main(String[] args){
ArrayList<String> mammals = new ArrayList<>();
mammals.add("Dog");
mammals.add("Cat");
mammals.add("Horse");
System.out.println("Mammals: " + mammals);
ArrayList<String> animals = new ArrayList<>();
animals.add("Crocodile");
// Add all elements of mammals in animals
animals.addAll(