bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java ArrayList add() Method

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java ArrayList add() Method?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ java.util.ArrayList;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The addAll() method adds all of the items from a collection to the list.
Definition and Usage
Java ArrayList add() Method

❮ ArrayList Methods

Example

import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    ArrayList<String> brands = new ArrayList<String>();
    brands.add("Microsoft");
    brands.add("ExampleSite");
    brands.add("Apple");
    brands.addAll(cars);
    System.out.println(brands);
  }
}

Definition and Usage

The addAll() method adds all of the items from a collection to the list.

If an index is provided then the new items will be placed at the specified index, pushing all of the following elements in the list ahead.

If an index is not provided then the new items will be placed at the end of the list.

One of the following

public boolean addAll(Collection<T> items )
public boolean addAll(int
index , Collection<T> items )

T refers to the data type of items in the list.

Parameter Values

ParameterDescription
indexOptional. The position in the list at which to add the items.
itemsRequired. A collection containing items to be added to the list.

Technical Details

Returns:true if the list changed and false otherwise.
Throws:IndexOutOfBoundsException - If the index is less than zero or greater than the size of the list. NullPointerException - If the collection is null.

Previous

Java ArrayList add() Method

Next

Java ArrayList clear() Method