Taking a method as a parameter of a method

I'm a c# developer but having to do some java at the moment. I need to write a method that takes a method as a parameter, and an array to use that method on. I guess in c# i'd just use a delegate, but not sure how to tackle this in java. So for example calling the method would look like:

methodName(methodToUseOnArray, arrayName)
mike_proAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You'd have to do that via reflection using the Method class, although i'm not sure why you'd want to. Looks like some kind of legacy function pointer thing - not good
0
colr__Commented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

for_yanCommented:
If you have just a limited number of methods from which you are choosing, I'd rather pass a String with method named and then have switch or if - else block
Inside your code, like
if(method.equals("method1"){
method1(...);
} else if (method.equals("method2"){
method2(...);
}
etc.

Not so fancy, but kind of simpler than
reflection
0
petiexCommented:
Here is something that avoids introspection. Instead of passing a method as a parameter, you pass a class that implements an interface whose contract requires a method that acts on an array.
public class TestClass {
    private String secretWord = "door";
    public void doThing(ArrayTaker klass, String[] arr) {
        klass.doArrayThing(arr);
    }

    interface ArrayTaker {
        public void doArrayThing(String[] arr);
    }

    class ArraySorter implements ArrayTaker {
        public void doArrayThing(String[] arr) {
            java.util.Arrays.sort(arr);
        }
    }
    class SecretWordReplacer implements ArrayTaker{
        public void doArrayThing(String[] arr){
            java.util.Arrays.fill(arr, arr.length - 1, arr.length, secretWord);
        }
    }

    class ArrayNuller implements ArrayTaker {
        public void doArrayThing(String[] arr) {
            for (int i = 0; i < arr.length; i++) {
                arr[i] = null;
            }
        }
    }

    public static void main(String... args) {
        TestClass tc = new TestClass();
        String[] arr = new String[]{"zoo", "horse", "cabbage"};
        tc.doThing(tc.new SecretWordReplacer(), arr);
        System.out.println(java.util.Arrays.toString(arr));
        tc.doThing(tc.new ArraySorter(), arr);
        System.out.println(java.util.Arrays.toString(arr));
        tc.doThing(tc.new ArrayNuller(), arr);
        System.out.println(java.util.Arrays.toString(arr));
    }
}

Open in new window

0
CEHJCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.