Java Reference Library →

LinkedList add() method - Syntax and Examples

LinkedList add() method - Syntax and Examples

Learn about the Java LinkedList `add()` methods, including how to add elements at the end of the list and insert elements at specific indices. Includes syntax, parameters, return values, and examples.

This reference explains LinkedList add() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

LinkedList add() method - Syntax and Examples

Java LinkedList add() method

The Java LinkedList is a versatile data structure that allows you to insert elements at any position. The `add()` methods are crucial for adding new elements to your linked list, either at the tail or at a specific index.

Syntax

public boolean add(E element)
public void add(int index, E element)

Parameters

Parameter Description
element The element to be added to the list.
index The index at which the specified element is to be inserted. Index must be in range 0 <= index <= size(). If index equals list’s size, the element is appended to end of list.

Return Value

  • add(E element): Returns true if the addition was successful; otherwise, returns false.
  • add(int index, E element): Returns nothing (void).

Examples

Adding an Element to the End of the List

This example demonstrates how to add a single element to the end of a LinkedList using the `add()` method.

import java.util.LinkedList;

public class LinkedListAddExample {
    public static void main(String[] args) {
        LinkedList<String> myList = new LinkedList<>();

        // Add elements to the end of the list
        myList.add("Apple");
        myList.add("Banana");
        myList.add("Cherry");

        System.out.println(myList); // Print the entire list
    }
}
[Apple, Banana, Cherry]

Here, we create a LinkedList of strings and add three fruits: "Apple", "Banana", and "Cherry". The `add()` method automatically appends these elements to the end of the list. The output shows the resulting linked list.

Adding an Element at a Specific Index

This example shows how to insert an element at a particular index within a LinkedList, using the `add(int index, E element)` method.

import java.util.LinkedList;

public class LinkedListAddAtIndexExample {
    public static void main(String[] args) {
        LinkedList<String> myList = new LinkedList<>();

        myList.add("Apple");
        myList.add("Banana");
        myList.add("Cherry");

        // Insert "Orange" at index 1
        myList.add(1, "Orange");

        System.out.println(myList); // Print the entire list
    }
}
[Apple, Orange, Banana, Cherry]

In this example, we start with a linked list containing "Apple", "Banana", and "Cherry". We then use `add(1, "Orange")` to insert "Orange" at index 1. Notice that the original element at index 1 ("Banana") is shifted one position to the right. The resulting list now contains "Apple", "Orange", "Banana", and "Cherry".

Adding an Element Beyond List Size

This example demonstrates what happens when you add an element at an index equal to the current size of the list.

import java.util.LinkedList;

public class LinkedListAddBeyondSizeExample {
    public static void main(String[] args) {
        LinkedList<String> myList = new LinkedList<>();

        myList.add("Apple");
        myList.add("Banana");

        // Add "Cherry" at index 2 (which is the size of the list)
        myList.add(2, "Cherry");

        System.out.println(myList); // Print the entire list
    }
}
[Apple, Banana, Cherry]

Here we add "Cherry" at index 2 which is the size of the linkedlist. This effectively appends the element to the end of list. The resulting list contains "Apple", "Banana", and "Cherry". It's equivalent to using `myList.addLast("Cherry")`.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →