Reputation: 131
Newbie here working a program where many methods repeat in somewhat the same manner, and I wonder if I can just make a method for this pattern with the different methods as parameter?
So basically for simplicity, let's say some methods need to repeat X times I need something like this, except this doesn't work:
public static void repeatThis(void method, int times){
for (int i = 0, i < times, i++){
method;
}
}
Which'd be a method to repeat the input method exactly times amount of times. But this doesn't work for good reasons. So how can I do something like this, whereas a parameter refers to a method which the first method should handle?
Upvotes: 1
Views: 1634
Reputation: 421100
So you want to pass "code" as an argument to a method. The way to do this in Java is to pass an object with you call a method on. If you don't have any arguments to the code you want to run nor is interested in any return value, you can use the Runnable
interface for this.
So, try this:
public static void repeatThis(Runnable runnable, int times) {
for (int i = 0; i < times; i++) {
runnable.run();
}
}
and invoke the method with
repeatThis(new Runnable() {
public void run() {
System.out.println("Your action");
}
}, 10);
If you're on Java 8 you can use lambdas and express it as follows:
repeatThis(() -> System.out.println("Your action"), 10);
Upvotes: 3
Reputation: 17319
The most basic thing you can do is create an interface for the method or use an existing interface like Runnable
.
public static void repeatThis(Runnable runnable, int times){
for (int i = 0, i < times, i++){
runnable.run();
}
}
Then to call it:
// Prints "Hi!" 5 times
repeatThis(new Runnable() {
@Override
public void run() {
System.out.println("Hi!");
}
}, 5);
Upvotes: 0
Reputation: 42174
There are a few ways to do this.
You could just pass in the Object that contains the method, then call the method on that Object. You'd have to hard-code the method ahead of time though.
Or you could pass in a wrapper Object that had a single function (called doFunction() or something) that simply called the correct function. You'd have to create the Object and pass into your repeat() function. (The other answers using Runnable are taking this approach).
Similarly, Java 8 closures do the above without having to use a concrete class.
Or you could use reflection.
Upvotes: 1
Reputation: 393946
Your method parameter should be of some interface type, and that interface should have a method, such as run(). Then you call that method in your loop.
Example :
public static void repeatThis(Runnable method, int times){
for (int i = 0, i < times, i++){
method.run();
}
}
That's assuming your method has no parameters and returns nothing. If that's not the case, you can use a different interface that contains a different method, with the parameters and return type you need.
In order to call repeatThis
, do something like :
repeatThis (
new Runnable () {
public void run()
{
//enter logic to be executed here
}
},100);
In Java 8 you can use lambda expressions instead of this anonymous class instance.
Upvotes: 1