Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing the Iterable interface

I just found this exam question in an old exam paper and am readying myself for an upcoming exam. I cannot figure it out :

The following depicts a contrived partial class which implements the Iterable interface. The only purpose of this class is to provide a method to iterate over the attribute things.

There are two things we need to fill in in the class to finish it. Here is the class

private class PartialIterableClass /*FILL IN */ {
   private String[] things;
   public PartialIterableClass( String[] things ){
      this.things = things;
   }
   /*FILL IN 2*/
}

I am guessing it should be something similar to :

private class PartialIterableClass implements Iterable<PrivateIterableClass> {
   private String[] things;
   public PartialIterableClass( String[] things ){
      this.things = things;
   }
   public Iterator<PartialIterableClass> iterator( ){
   return new Iterator<PartialIterableClass>( ) {

   }
   }
}

I'm not really sure how to flesh out an answer to this question though, can anybody help?

like image 785
John Curtsy Avatar asked May 18 '11 20:05

John Curtsy


1 Answers

Your Iterator must implement all the methods from the Iterator interface in order to encapsulate the iteration logic.

In your case, it will have to hold the current iteration index in the array. You can look at ArrayIterator from commons-collections

like image 61
Bozho Avatar answered Sep 23 '22 19:09

Bozho