Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java 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")`.


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M