Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

896 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

13 Experts available now in Live!

Get 1:1 Help Now