java - Adding ".foreach" capability to a custom container class -
i wrote own container class game, similar of arraylist different in quite few ways, anyhow want write foreach method iterate on backing array.
i know use arrays.stream
i'm curious how write custom lambda implementation of #foreach method iteration on array.
anybody have clue? thanks
example:
class container<t> { t[] array = new t[200]; }
now instance lets wanted this:
container<fish> fishies = new container(); fishies.foreach(fish->system::out);
you need implement foreach
method similar of stream
interface in container
class :
void foreach(consumer<? super t> action) { (int = 0; < array.length; i++) action.accept(array[i]); }
this foreach
implementation serial, it's simpler stream
implementations, can parallel.
i'm ignoring fact t[] array = new t[200];
doesn't pass compilation, that's different issue.
Comments
Post a Comment