• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

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)
0
mike_pro
Asked:
mike_pro
1 Solution
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now