Java ArrayList Class

本文详细介绍了Java中的ArrayList类,包括其与数组的区别、创建方式、常用方法,如添加、初始化、访问、修改、删除元素,以及遍历、获取长度、排序等操作,并给出了丰富的示例代码。
摘要由CSDN通过智能技术生成
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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值