Solved

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

Posted on 2004-04-16
5
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

705 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