In this tutorial we will learn about the Iterator interface and its use with the collection interface.We will create an example to print the content of the collection using the iterator() method.
In this tutorial we will learn about the Iterator interface and its use with the collection interface.We will create an example to print the content of the collection using the iterator() method.
import java.util.*; public class collection { public static void main(String[] args) { Collection c = new ArrayList(); String names[] = { "jack", "rock", "mac", "joe" }; for (int i = 0; i < names.length; i++) { c.add(names[i]); } Iterator it = c.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }
Output
jack rock mac joe