# Java `ArrayList add()`方法

> 原文： [https://beginnersbook.com/2013/12/java-arraylist-add-method-example/](https://beginnersbook.com/2013/12/java-arraylist-add-method-example/)

这里我们讨论`Java.util.ArrayList`类的`add()`方法。此方法用于向`ArrayList`添加元素。以下是方法签名：

`public boolean add(Object element)`

例

```java
package beginnersbook.com;
import java.util.ArrayList;
public class Details {
    public static void main(String[] args) {

        //ArrayList<String> Declaration
        ArrayList<String> al= new ArrayList<String>();
        //add method for String ArrayList
        al.add("Ram");
        al.add("Shyam");
        al.add("CPS");
        al.add("John");
        al.add("Steve");
        System.out.println("Elements of ArrayList of String Type: "+al);

        //ArrayList<Integer> Declaration 
        ArrayList<Integer> al2 = new ArrayList<Integer>();
        //add method for integer ArrayList
        al2.add(1);
        al2.add(34);
        al2.add(99);
        al2.add(99);
        al2.add(78);
        System.out.println("Elements of ArrayList of Integer Type: "+al2);
    }
}
```

输出：

```java
Elements of ArrayList of String Type: [Ram, Shyam, CPS, John, Steve]
Elements of ArrayList of Integer Type: [1, 34, 99, 99, 78]
```

#### 参考：

[`ArrayList.add(E)`](https://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(E))