import java.util.LinkedList; import java.util.Iterator; public class LinkedListIteratorExample { public static void main(String[] args) { LinkedList<String> fruits = new LinkedList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Cherry"); Iterator<String> iterator = fruits.iterator(); System.out.println("Using Iterator to traverse through the LinkedList:"); while (iterator.hasNext()) { String fruit = iterator.next(); System.out.println(fruit); } System.out.println("\nUsing for-each loop to traverse through the LinkedList:"); for (String fruit : fruits) { System.out.println(fruit); } iterator = fruits.iterator(); // Reset the iterator while (iterator.hasNext()) { String fruit = iterator.next(); if (fruit.startsWith("B")) { iterator.remove(); } } System.out.println("\nLinkedList after removal of elements that start with 'B':"); for (String fruit : fruits) { System.out.println(fruit); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter