Java LinkedList offerLast() Method
Example
Add an item to the end of a list:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.offerLast("Mazda");
System.out.println(cars);
}
}
Definition and Usage
The offerLast() method adds an item to the end of the list.
It works like addLast(), but it also returns true when the item is added.
Tip: Use the offerFirst() method to add an item to the beginning of the list.
Syntax
public boolean offerLast(T item)
T refers to the data type of items in the list.
Parameter Values
| Parameter | Description |
|---|---|
| item | Required. The item to add to the end of the list. |
Technical Details
| Returns: | true, because the item is always added. |
|---|
Related Pages
❮ LinkedList Methods