Solved

Is it possible to simultaneously call a method on an array?

Posted on 2004-04-16
5
159 Views
Last Modified: 2010-03-31
for example, is anything like this possible?

arrayofObjects[].callMethod();

or is a loop needed?
0
Comment
Question by:aidan_b5
5 Comments
 
LVL 9

Accepted Solution

by:
Nick_72 earned 20 total points
ID: 10843243
no, that is not possible, you have to call the method on each element in the array.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10844678
If the calls are very time consuming and there aren't that many objects in the array, you could spawn threads to perform each call.
0
 
LVL 7

Expert Comment

by:mark-b
ID: 10845186
Just a comment here-- I'm not sure what your objective is.  Is it to call a method on every element of the array or to call ONE method on the array class?

If it is on every element of the array, then Nick is right.  However, if your objective is to call ONE method that serves a specific purpose in relation to the array as an entire set, then I suggest you create a wrapper class-- something like this:

public class SomeArray extends ArrayList {
  public SomeArray( Object[] array ) {
    super( Arrays.asList( array ) );
  }

  public void printThem() {
    for( Iterator i = iterator(); i.hasNext(); ) {
      System.out.println( i.next() );
    }
  }
}      

and you could use it like this:

SomeArray sa = new SomeArray( new String[] { "a", "b", "c" } );
sa.printThem(); // operates on all elements
System.out.println( sa.size() );

This gives you a couple of advantages:
  1) Your ability to perform operations on all elements in the set
  2) Use methods already available on ArrayList
  3) Add your own methods for whatever purpose, for example-- say your array is a homogenous array of type Dog, which all have a bark() method-- then you could add a method of the same name to this class to iterate through all the elements and invoke it on each one:

public void bark() {
    for( Iterator i = iterator(); i.hasNext(); ) {
      ( (Dog) i.next() ).bark();
    }
}

Just some thoughts...

-Mark
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now