Through this example, we may see how to iterate over all elements of LinkedList in Java.
Source Code
package com.beginner.examples;
import java.util.LinkedList;
public class IterateLinkedListExample {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
//Add elements
ll.add("a");
ll.add("b");
ll.add("c");
ll.add("d");
ll.add("e");
ll.add("f");
ll.add("g");
//Gets the number of elements to iterate through
System.out.println("Elements:");
for(int i=0 ;i<ll.size();i++)
{
String element = ll.get(i);
System.out.println(element);
}
}
}
Output:
Elements:
a
b
c
d
e
f
g
References
Imported packages in Java documentation: