Re: iterate through methods

From: xarax (xarax_at_email.com)
Date: 08/05/04


Date: Thu, 05 Aug 2004 00:24:52 GMT


"Christophe Vanfleteren" <c.v4nfl3t3r3n@pandora.be> wrote in message
news:3ycQc.203748$py4.10431543@phobos.telenet-ops.be...
> JCary wrote:
>
> > I would like to iterate through several methods and I cannot seem to find
> > a
> > way. Can someone please point me in the right direction. Can this even
> > be done.
> >
> > Thanks,
> >
> > Cary
> >
> > ** Idea of what I'm trying to accomplish
> >
> > private void methodIterator(){
> > String m;
> > for(int i = 0; i <= 10; i++){
> > m = "method_" + String.valueOf(i) + "()";
> > // Somehow execute method here
> > }
> > }
> <snip method>
>
> I'm sure a loop of less methods would have made your problem clear :)
>
> Anyway, most of the time it is pretty suspicous when you (think) you need
> something like this, but it can be done using reflection.
>
> Look at the java.lang.reflect package and
>
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getDeclaredMethods
()>

And if you think your problem requires reflection,
then think again, and think again, and think again
before using reflection.

For example, in your problem, is the string the
determining factor for which method is called or
is it the index? You problem looks like it is
index-driven, rather than name driven. Therefore,
an array of interface references indexed by the
loop variable would solve your problem and run
far more efficiently than reflection.

In C, an array of function pointers would be
indexed in the loop. In Java, the equivalent
approach is array an interface references indexed
in the loop. It's a little more complicated to
set up than C, but it's definitely very efficient.



Relevant Pages

  • Re: for loop problem
    ... I'm an old school C guy just learning perl. ... So your problem is not actually with the construct of the for loop. ... iterate over an array using index values. ...
    (perl.beginners)
  • Re: missing values from files
    ... This will loop 14 times, ... If you want to loop over an array use the ... You should only use a range if you want to iterate a specific number ...
    (perl.beginners)
  • Re: (loop for foo across bar) and multiple-dimensional arrays
    ... | I noticed that both SBCL and CLISP refuse to iterate over multi-dimensional ... Go to 6.2 The Iteration Dictionary and read about the Macro LOOP ... which defines a vector to be a one-dimensional array. ...
    (comp.lang.lisp)
  • Re: substring finding problem!
    ... do matrix multiplication without using the '*' operator. ... how about we iterate through an array without using a for loop, ...
    (comp.lang.c)
  • Re: method iteration
    ... Cary Robins scribbled the following: ... > Is there anyway to iterate through like methods in a loop. ... Anyway, you could use Reflection to do what you want, but I still think ...
    (comp.lang.java.programmer)