This example show you how to use the Iterator
interface to iterate elements of a LinkedList
.
package org.kodejava.util;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
public class LinkedListIterator {
public static void main(String[] args) {
List<String> grades = new LinkedList<>();
grades.add("A");
grades.add("B");
grades.add("C");
grades.add("D");
grades.add("E");
// Iterates elements in LinkedList using Iterator.
Iterator<String> iterator = grades.iterator();
while (iterator.hasNext()) {
System.out.println("Grade: " + iterator.next());
}
}
}
This program prints the following output:
Grade: A
Grade: B
Grade: C
Grade: D
Grade: E
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024