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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With