Java String Array Iterator Example

Problem

Given the signature of the Java method below

Provide implementation that iterates through each string in the order that they are provided but it skips null values

Solution

We implement the Iterable interface. We add a class member variable to store the list values in an array of strings. We override hasNext, next and remove methods. hasNext method keeps track of the current position in list as we iterate. next method was modified such that null values are skipped. remove method is kept intact. Here is the code

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *