Java Two Dimensional String Array Iterator

Problem

Given the signature of the method below. It receives a two dimensional array of strings. Provide implementation that iterates through each string in each sub-array in the order they are provided but it skips null values

Solution

We are dealing with a two dimensional array of strings so we need a proper way to loop through it. It is a common task in programming. You just fix the row then loop in the column if you want to process it row by row. The … here represents the rows and the [] represents the columns and the intersection is a given string that needs to be returned if not null.

  • hasNext: as long as the row index is less than number of rows and column index is less than number of columns then there are elements to process
  • next: loop in a two dimensional array of strings row by row

Here is the code to do that

Tags:

Add a Comment

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