bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java LinkedList add() Method

❮ LinkedList Methods

Example

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.add("Mazda");
    System.out.println(cars);
  }
}

Definition and Usage

The add() method adds an item to the list.

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

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

One of the following

public boolean add(T
item )
public void add(int
index , T
item )

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 item.
itemRequired. The item to be added to the list.

Technical Details

Returns:Nothing if an index is specified. When an index is not specified it returns true if the list changed and false if the list did not change.
Throws:IndexOutOfBoundsException - If the index is less than zero or greater than the size of the list.

Previous

Java LinkedList Methods

Next

Java LinkedList add() Method