Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Doing One Operation on All Array Members (Without Iteration)

Thread view: 
Hal Vaughan - 08 Sep 2006 08:54 GMT
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?

Thanks!

Hal
Hendrik Maryns - 08 Sep 2006 09:42 GMT
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.
[quoted text clipped - 13 lines]
>
> 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.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
M.J. Dance - 08 Sep 2006 10:56 GMT
> Hal Vaughan schreef:
>> I don't think this is possible, but I figure it won't hurt to ask.  I just
[quoted text clipped - 23 lines]
> 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.

<WishfullThinking>
Come to think of it, Mathematica has some powerful features related to such a
notion: http://documents.wolfram.com/mathematica/functions/Map (see also Map*,
Apply and Operate on the same page). I hear Java is eyeing closures for one its
future versions - maybe it could come with Map*, Apply and/or Operate at the
same time.
</WishfullThinking>
M.J. Dance - 08 Sep 2006 11:02 GMT
> Appart from utilizing multiple threads / CPUs to do the job faster is
> possible,

Make that _if_ possible.
Oliver Wong - 08 Sep 2006 16:29 GMT
>> Hal Vaughan schreef:
>>> I don't think this is possible, but I figure it won't hurt to ask.  I
[quoted text clipped - 38 lines]
> guess it's down to generics: making sure, that all the entries are
> suitable.

   Presumably, myObject has a type, and the operations you could perform on
myObject[] would be limited to those defined on the type of myObject.

List[] myLists = /*initialize somehow*/

myLists.toString(); /*Calls the method defined on the array*/
myLists[].toString(); /*Calls the method n times, on each element in
myList*/

myLists[].add(null); /*This works, as add(Object) is defined in the
interface List*/
myLists[].dance(); /*Compile time error, as the interface List doesn't
define a method "dance"*/

   - Oliver
M.J. Dance - 11 Sep 2006 07:58 GMT
>> Of course, implementing (& using) such a feature could be problematic.
>> For example, having an array containing instances of objects not all
[quoted text clipped - 17 lines]
> myLists[].dance(); /*Compile time error, as the interface List doesn't
> define a method "dance"*/

Sure. But consider this.

List<Perform> list = ...;
Object[] array = list.toArray();

Although Perform could implement .dance(), one couldn't call array.dance(),
because Object (and thus Object[]) doesn't.

There are many such cases in Java API. Until generics, those seemed reasonable.
Now we have a strange mixture.
Manish Pandit - 08 Sep 2006 09:44 GMT
Hi,

There is a forAllDo(Collection c, Closure c) in Apache Commons
CollectionUtils that might help you, but I am quite positive it ends up
iterating the collection anyway..just a little more fancy ;-)

http://jakarta.apache.org/commons/collections/apidocs/org/apache/commons/collect
ions/CollectionUtils.html


-cheers,
Manish

> I don't think this is possible, but I figure it won't hurt to ask.  I just
> might learn something new.
[quoted text clipped - 17 lines]
>
> Hal
Daniel Dyer - 08 Sep 2006 12:18 GMT
> I don't think this is possible, but I figure it won't hurt to ask.  I  
> just
[quoted text clipped - 15 lines]
>
> I don't think this is possible, but is there a way to do it?

What you are probably looking for is something like the functional  
programming concept of "higher-order functions" (in particular the map  
function of languages like Haskell and Miranda).  Java doesn't really do  
this (although something similar can be hacked with objects, as it seems  
to have been in the Jakarta Commons link posted by Manish).  Regardless,  
this just hides the looping from you, something under the covers will  
still need to make sure that the operation is applied to each element.

Joel had an interesting article on first class functions a few weeks ago  
(http://www.joelonsoftware.com/items/2006/08/01.html).  The article on  
Java that he links to, "Execution in the Kingdom of the Nouns"  
(http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html),  
is also well worth reading.

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Stefan Ram - 08 Sep 2006 13:44 GMT
>myObject[].setActive(true)

 An approximation would be as follows:

interface Operation<Domain>{ void of( Domain value ); }

class Thing
{ public void setActive( final java.lang.Boolean booleanValue )
 { java.lang.System.out.println( "setActive." ); }}

class Array<T>
{ final T[] value;
 Array( final T[] value ){ this.value = value; }
 public void map( final Operation<T> operation )
 { for( final T t : value )operation.of( t ); }}

public class Main
{
 static final Array<Thing> things = new Array<Thing>
 ( new Thing[]{ new Thing(), new Thing(), new Thing() });

 public static void main( final java.lang.String[] commandLineArguments )
 {
   things.map( new Operation<Thing>()
     { public void of( final Thing thing ){ thing.setActive( true ); }}); }}

setActive.
setActive.
setActive.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.