Solved

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

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

742 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