How to Remove Specified Element from Java LinkedList


In this example we will show how to remove a particular element from LinkedList in Java.

Source Code

package com.beginner.examples;

import java.util.LinkedList;

public class RemoveElementLinkedList {

	public static void main(String[] args) {
		
		//create LinkedList.
		LinkedList linkedList = new LinkedList();
		
		linkedList.add("A");
		linkedList.add("B");
		linkedList.add("C");
		linkedList.add("D");
		
		System.out.println("Before remove, linkedList is :" + linkedList);
		
		/*
		 * To remove an element at specified index of LinkedList, use
                 * remove(int index) method.
		 */
		linkedList.remove(2);
		
		System.out.println("After remove, linkedList is : " + linkedList);
		

	}

}

Output:

Before remove, linkedList is :[A, B, C, D]
After remove, linkedList is : [A, B, D]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments