"M.J. Dance" <mjdance@hotmail.com> wrote in message
Hendrik Maryns wrote:
Hal Vaughan schreef:
I don't think this is possible, but I figure it won't hurt to ask. I
just
might learn something new.
I have an object I've created with a boolean isActive. I can set it my
setting the field directly (myObject.isActive = true) or with a setter
(myObject.setActive(true)). I will be working with an array, like this:
myObject[]
Is there any way to do the same operation on all members of that array
without using a loop to iterate through each item in the array? In this
case, I'd like to set all the members to active at once. It would be
like
doing:
myObject[].setActive(true)
I don't think this is possible, but is there a way to do it?
How would you imagine this to work? Suppose, just for a moment, that
myObject[].setActive(true)
were defined. Then how would it be supposed to work? Unless you have a
multi-CPU machine, it will have to go through the array sequentially
anyway, so you might as well do the looping yourself, and do
null-checking as well in the meantime.
Appart from utilizing multiple threads / CPUs to do the job faster is
possible, the main point is to remove unnecessary code, thereby making it
shorter, simpler, easyer to read and, in final consequence, getting rid of
possible hiding places for bugs.
Of course, implementing (& using) such a feature could be problematic. For
example, having an array containing instances of objects not all of which
have a .setActive(boolean) method. OK, an OperationNotSupportedException
could be thrown, which means one would have to catch it somewhere and
those few lines of code we were trying to get rid of, are here again. So I
guess it's down to generics: making sure, that all the entries are
suitable.
myObject[] would be limited to those defined on the type of myObject.