Explanation of Solution
Iterator:
Iterator is used to access data in the collections without relieving how data stored in the data structure.
Iterator interface has the “iterator()” method, which returns iterator object. Iterator method is used for traversing data in an orderly fashioned manner.
Data in the collections that can be accessed using the following three methods,
- next() – Accessing the next elements in the iterator.
- hasNext() – To check more elements are available in the iterator.
- remove() – To delete the last element returned by the iterator.
Example:
//Import the java package
import java.util.*;
public class Sample
{
//Main method
public static void main(String[] args)
{
//Creating new array list
Collection<String> collection = new ArrayList<>();
//Adding new elements to the list
collection.add( "Apple" );
collection.add( "Orange" );
collection ...
Want to see the full answer?
Check out a sample textbook solutionChapter 19 Solutions
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)