Solved

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

Posted on 2004-04-16
5
176 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JavaScript/Java - Changing an image background color 4 65
Way to decrease size of apk file 9 65
servlet example 17 28
runtime exception 2 27
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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 …

813 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

10 Experts available now in Live!

Get 1:1 Help Now