How to get element of LinkedList

We  use method  get(int index) for get any element at specific index.

import java.util.LinkedList;

public class GetElement {

public static void main(String[] args) {

LinkedList<String> linkedList = new LinkedList<String>();

linkedList.add(“Mumbai”);
linkedList.add(“Delhi”);
linkedList.add(“Noida”);
linkedList.add(“Gao”);
linkedList.add(“Patna”);

//print linkedlist
System.out.println(“Before Remove Element Linklist is = “+linkedList);

System.out.println(“Element At Index 2 =”+linkedList.get(2));

}

}

Output:

Before Remove Element Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]

Element At Index 2 =Noida

Leave a Reply

Your email address will not be published. Required fields are marked *

− 7 = 3