java iterable pencil - How to make custom collections iterable?

How to make custom collections iterable?

Posted on

java iterable pencil - How to make custom collections iterable?The advantages of utilizing an iterator is to permit the the consumer to iterate over the gathering of objects, with out exposing implementation particulars. This provides you the profit to vary what assortment the Iterator iterates over and the way every aspect is served, with out making any change the consumer’s code. To iterate a customized assortment, class have to implements Iterable interface.

A category that implements the Iterable can be utilized with the brand new for-loop. Right here is such an instance:

The Iterable interface has just one technique:

Now create a java class so as to add into that ArrayList

Now, create a category PencilArrayList which implements Iterable interface.

Supply techzoo.org