Monday 18 May 2015

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

No comments:

Post a Comment