In this example we are gonna see how to get an element from specific index of LinkedList using get(int index) method:
public E get(int index): Returns the element at the specified position in this list.
import java.util.LinkedList;
public class GetElementExample {
public static void main(String[] args) {
// Creating LinkedList of String Elements
LinkedList<String> linkedlist = new LinkedList<String>();
// Populating it with String values
linkedlist.add("AA");
linkedlist.add("BB");
linkedlist.add("CC");
linkedlist.add("DD");
linkedlist.add("EE");
System.out.println("LinkedList Elements : ");
//get(i) returns element present at index i
for(int i=0; i < linkedlist.size(); i++){
System.out.println("Element at index "+i+" is: "+linkedlist.get(i));
}
}
}
Output:
LinkedList Elements : Element at index 0 is: AA Element at index 1 is: BB Element at index 2 is: CC Element at index 3 is: DD Element at index 4 is: EE
Leave a Reply